Similarities between ArrayList and LinkedList
Java ArrayList and Java LinkedList both implements List interface. Both can dynamically grow and makes proper use of memory. Both looks same from outside but there are some advantages of one over other. So, what is the difference between ArrayList and LinkedList and which one to choose for making the better application?
Both maintain insertion order which means when you add elements in an ArrayList or in LinkedList, they will be added in same sequence in which they are added. Same in case of the delete operation.
Difference between Arraylist and LinkedList
As we know, ArrayList maintains an index of each element as it uses array data structure internally whereas, Java LinkedList implements Doubly Linked List which uses two side pointers.
So for search operations, ArrayList performs O(1) operation as it knows an index of an element but in case of LinkedList, it has to search for the specific node first which in worst case maybe performs O(n) operations.
If your application requires more search operations(Lookup), you can choose ArrayList in your application rather than LinkedList.
ArrayList in case of insertion requires more cost in terms of memory as it has to shift elements every time during the insertion operation. In the worst case, it would be O(n) operations while in the best case there will be O(1) operations.
In the case of LinkedList, insertion would be easy as it maintains pointers .
If your application requires more insert operations, you can choose LinkedList in your application rather than ArrayList.
ArrayList deletion also requires more cost in terms of memory as it has to shift elements every time during the delete operation. In the worst case, it would be O(n) operations if that element is last while in the best case there will be O(1) operations if an element is at first position.
In the case of LinkedList, deletion would be easy as it maintains pointers . Just need to change two node pointers.
If your application requires more delete operations, You can choose LinkedList in your application rather than ArrayList.
Largest collection of up-to-date tutorials to learn programming languages. We are focused on easy learning. Massive collection of interview questions one may need for preparation.