The table below summarizes the comparisons between ArrayList vs LinkedList : ArrayListĪrrayList is a class in a collection framework that uses a dynamic array to store its elements. Comparison Table of ArrayList vs LinkedList Whereas in the case of LinkedList, memory is allocated from heap memory that is used to allocate memory to the variables at run time. Memory Storage Type: Since memory is allocated to the ArrayList at the compile-time thus, Stack memory is used. Thus it is easier to expand the size of the list in case LinkedList than ArrayList.Ħ. Also, the memory location where the elements in LinkedList need not be contiguous. Whereas in the case of LinkedList, memory is allocated run time, also known as dynamic memory allocation. Also, the memory allocated is contiguous.Įxample: Consider below ArrayList A and the memory address of its elements. This is known as Static memory allocation. Memory Allocation: Memory to ArrayList is allocated at the compile time itself thus, it is compulsory to specify the size of the list before execution. Thus it is more efficient in this case.ĥ.
In case we use LinkedList, deletion can be performed with O(1) of time complexity as the memory of the node needs to deallocated and pointers of the previous and next node needs to update only. Deletion: In the case of Deletion also, ArrayList takes more time since it needs to copy the elements to the new array at updated locations thus have time complexity of O(n). Whereas in the case of LinkedList needs to traverse the complete list to access the element.Įxample: Accessing the fourth element in ArrayList A, we just need to mention A as the index in case of an array starts with 0.Ĥ. Data Access: In case one needs to access an element at a location, ArrayList is more efficient in this case since it uses indexes to store the elements and can be easily accessed using the particular index. Thus in case insertion or deletion operation needs to be performed often thus one must choose LinkedList.ģ. Thus time complexity is O(1) for insertion operation.
In the case of LinkedList, insertion operation is more efficient as memory is variable and allocated dynamically, and no shifting of the element is required instead, only pointers need to be updated. In the case of ArrayList, when one element needs to be added at the particular index of the list, it is comprised of 2 methods- expansion of the array size with a new size and copying the elements to the newer array at an updated location.
Insertion: Insertion operation comprises of the addition of an element in the existing list. Type of Elements: ArrayList is used to store homogeneous elements, but LinkedList can be used to store heterogeneous elements also.Ģ. Let us discuss some key differences between ArrayList vs LinkedList in the following points:ġ. Key differences between ArrayList vs LinkedList
Web development, programming languages, Software testing & others
It is a very common pattern for solving array related problems.Start Your Free Software Development Course This approach is called the two-pointer approach. if arr + arr if arr + arr = target, then return the indices.Loop until left Initialize two variables, one pointing to the beginning of the array ( left) and another pointing to the end of the array ( right).There is another approach which works when you need to return the numbers instead of their indexes. Use Sorting along with the two-pointer approach