Is the Remove method of LinkedList in Java really time consuming O (1)?

Source: Internet
Author: User

This problem actually stems from a leetcode topic, that is, the previous log LRU Cache. After using the LinkedList timeout, instead of ArrayList AC, and the problem is actually List.remove (Object O) This method.

We know that the main feature of the list and array is that the add and remove operations are O (1). The list of links in Java generally uses the LinkedList type, and arrays generally use ArrayList. They also implements the list of the interface, so there are both the Remove (int index) and the Remove (Object O) methods.

The general sense is that the remove operation of the list is O (1), because for a given node, the next of its predecessor node can be placed directly on node. In the array, you need to delete the element at index and then move the next n elements forward, so you need O (n) time.

But is it true in Java?

Look at the source of the JDK, you can find that LinkedList's remove (int index) and remove (Object O) Both methods do not do O (1) time, but O (n). This is because the O (1) time in the data structure mentioned above is for a certain node that has been identified. In LinkedList, you first have to go through a loop, find the first object o that appears, or walk to the index position, and then proceed. That is, there is a get process.

At this point, although ArrayList's remove (int index) and remove (Object O) are also O (n) time, the movement takes much more time than the LinkedList in the back, especially when there are many elements. In the JDK source code, this operation is done using system.arraycopy (). So, at this time, LinkedList most of the pit dad's place, but also the most puzzling place on the--remove of the operation is actually slower than ArrayList, and more slowly!

Furthermore, LinkedList needs to maintain a data structure internally, called node in JDK 6, ENTRY,JDK 7, which requires a lot of extra memory. Therefore, unless the LinkedList deque function is urgently needed, the ArrayList should be used in any case. In fact, even if you want to use deque, there are arraydeque.

So, sometimes the interview asks, in a linkedlist list's traverse for loop, what is the time complexity of the remove (i) operation constantly executing? It's actually O (n^2), not O (n). However, if the time complexity of using iterator,it.remove () is O (1), because the element is already given. Also, the Remove (i) operation in the For loop is to affect the subscript. After remove, I must be--。 every time I Using iterator can effectively avoid this problem. As you can see here, although the For loop is intuitive, sometimes iterator is very good.

Is the Remove method of LinkedList in Java really time consuming O (1)?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.