InterviewSolution
| 1. |
What is the difference between an ArrayList and LinkedList? |
|
Answer» ArrayList and LinkedList both represent a list of numbers but differ in their internal implementation. ArrayList uses an array internally to store the elements added to it. When the number of elements is about to exceed the size of the array, it allocates a NEW array and copies the elements to the new location. It GIVES constant time access to add (if it doesn’t need to expand) and GET an element, but for deletion, it gives linear time COMPLEXITY as it needs to shift its elements to the left. LinkedList, on the other hand, MAINTAINS a sequence of linked nodes internally for storing the elements. So, retrieves an element in linear time whereas addition and deletion take a constant time to execute. |
|