☕ Java Intermediate

What is the difference between ArrayList and LinkedList?

Answer

Both implement the List interface but use different internal data structures. ArrayList uses a dynamic array — random access is O(1) (get by index is instant), but insertion/deletion in the middle is O(n) because elements must be shifted. It is better for read-heavy workloads and when frequent index-based access is needed. LinkedList uses a doubly-linked list — insertion/deletion at head or tail is O(1), but random access is O(n) because you must traverse from the start. LinkedList also implements Deque, so it can be used as a stack or queue. In practice, ArrayList outperforms LinkedList for most use cases due to better cache locality.