Advertisement
Intermediate Java, Collections Framework

What are the key performance differences between ArrayList and LinkedList for common operations (random access, insertion/removal at various positions), and when would you choose one over the other?

Short answer

ArrayList backs its elements with a resizable array, giving O(1) random access (get(index)) but O(n) insertion/removal at arbitrary positions (requires shifting subsequent elements); LinkedList uses a doubly-linked list of nodes, giving O(1) insertion/removal once you already…

Save Java mock interview
Advertisement
AModel answer
ArrayList backs its elements with a resizable array, giving O(1) random access (get(index)) but O(n) insertion/removal at arbitrary positions (requires shifting subsequent elements); LinkedList uses a doubly-linked list of nodes, giving O(1) insertion/removal once you already have a reference to the position (like via an iterator) but O(n) random access (must traverse from an end) -- in practice, ArrayList is the better default choice for the vast majority of use cases due to better cache locality and lower per-element memory overhead, with LinkedList's specific advantages rarely outweighing these in modern JVM/hardware characteristics.
List<Integer> arrayList = new ArrayList<>();
List<Integer> linkedList = new LinkedList<>();

// O(1) for ArrayList, O(n) for LinkedList
arrayList.get(500);

// O(n) for ArrayList (shifts elements), O(1) for LinkedList IF you already have the position via iterator
arrayList.add(0, value);  // insert at front, expensive for ArrayList
Real-world example
A performance review of a codebase using LinkedList by default (under the mistaken assumption it's generally faster for insertions) finds that switching to ArrayList for the vast majority of use cases actually improved performance, since the workload was dominated by iteration and random access rather than frequent middle-of-list insertion.
Likely follow-ups
  • Why does LinkedList's theoretical O(1) insertion advantage rarely materialize in practice for common use patterns?
  • What's the actual per-element memory overhead difference between ArrayList and LinkedList?
Related topics