Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insertion in the middle of ArrayList vs LinkedList [duplicate]

Talking in Java's context. If I want to insert in the middle of either an ArrayList or a linkedList, I've been told that Arraylist will perform terribly.

I understand that it is because, we need to shift all the elements and then do the insertion. This should be of the order n/2 i.e. O(n).

But is not it the same for linkedList. For linked List, we need to traverse till the time we find the middle, and then do the pointer manipulation. In this case too, it will take O(n) time. Would not it?

Thanks

like image 582
Kraken Avatar asked Oct 09 '13 15:10

Kraken


People also ask

Which is more efficient inserting an element in the middle of an ArrayList or inserting a node into the middle of a LinkedList?

Linked lists are preferable over arrays when you want to be able to insert items in the middle of the list (such as a priority queue). ArrayList is slower because it needs to copy part of the array in order to remove the slot that has become free.

Which is better for insertion ArrayList or LinkedList?

ArrayList is faster in storing and accessing data. LinkedList is faster in manipulation of data.

How do ArrayList and LinkedList behave when adding an element in the middle of them?

For ArrayList , insertion is O(1) only if added at the end. In all other cases (adding at the beginning or in the middle), complexity is O(N), because the right-hand portion of the array needs to be copied and shifted. The complexity of a LinkedList will be O(1) both for insertion at the beginning and at the end.

Which gives better performance for inserting and deleting in the middle of the list?

If you are performing insertion of data in the middle or deletion of data in the middle is relatively easy, hence go for LinkedList. It works best for frequent addition or deletion in the middle.


1 Answers

The reason here is that there's no actual shifting of elements in the linked list. A linked list is built up from nodes, each of which holds an element and a pointer to the next node. To insert an element into a list requires only a few things:

  1. create a new node to hold the element;
  2. set the next pointer of the previous node to the new node;
  3. set the next pointer of the new node to the next element in the list.

If you've ever made a chain of paper clips, you can think of each paper clip as being the beginning of the chain of it and all the paper clips that come after it. To stick a new paper clip into the chain, you only need to disconnect the paper clips at the spot where the new one will go, and insert the new one. A LinkedList is like a paper clip chain.

An ArrayList is kind of like a pillbox or a mancala board where each compartment can hold only a single item. If you want to insert a new one in the middle (and keep all the elements in the same order), you're going to have to shift everything after that spot.

The insertion after a given node in a linked list is constant time, as long as you already have a reference to that node (with a ListIterator in Java), and getting to that position will typically require time linear in the position of the node. That is, to get to the _n_th node takes n steps. In an array list (or array, or any structure that's based on contiguous memory, really) the address of the _n_th element in the list is just (address of 1st element)+n×(size of element), a trivial bit of arithmetic, and our computing devices support quick access to arbitrary memory addresses.

like image 190
Joshua Taylor Avatar answered Nov 09 '22 16:11

Joshua Taylor