ArrayList vs LinkedList

Difficulty: Intermediate

Question

What is the difference between ArrayList and LinkedList? When would you actually choose LinkedList?

Answer

This question has a textbook answer and a real-world answer, and the interviewers who care will push you toward the second. Let me give you both.

The textbook answer first. ArrayList is backed by a dynamic array (Object[]). Because elements live in contiguous memory, get(i) and set(i) are O(1). Adding at the end is amortized O(1): when the array is full, ArrayList allocates a new array 1.5 times larger (oldCapacity plus half of it) and copies the elements over. Inserting or removing in the middle is O(n) because later elements must shift. The default capacity is 10, created lazily on the first add in Java 8 and later.

LinkedList is a doubly linked list. Each element is a node holding the value plus references to the previous and next nodes. Adding or removing at either end is O(1), and once you hold a node (or an iterator positioned at it), inserting or removing there is O(1). But get(i) is O(n) because you must walk from the nearest end. It also implements Deque, so it can serve as a queue or stack.

Now the real-world answer, which is the one that shows experience. The claim that LinkedList is faster for insertions in the middle is misleading. To insert in the middle by index, you first have to find that position, which is O(n) traversal with pointer chasing. ArrayList does the shifting using System.arraycopy, which is a highly optimized memory copy. Because of CPU cache locality, array traversal and copying are extremely fast, while linked nodes are scattered across the heap, causing cache misses. In benchmarks, ArrayList usually beats LinkedList even for middle insertions until lists get very large. Memory also favours ArrayList: each LinkedList node carries an object header plus two references on top of the payload, easily 24 to 40 bytes of overhead per element.

So when do you genuinely choose LinkedList? Rarely. Its advantage shows up when you repeatedly insert or remove through an iterator during traversal, or when you frequently add or remove at the head. But even for queue and stack use, ArrayDeque is typically faster and lighter than LinkedList, so it is the recommended Deque implementation. Josh Bloch, who wrote LinkedList, has famously said he never uses it.

Some subtle points to include. Both allow duplicates and nulls, both maintain insertion order, and neither is thread-safe. Wrap them with Collections.synchronizedList or use CopyOnWriteArrayList for concurrency. Both have fail-fast iterators. ArrayList implements RandomAccess, a marker interface that algorithms in Collections check to choose an index-based or iterator-based strategy.

A classic pitfall involves remove on a List of Integer. list.remove(1) removes the element at index 1, while list.remove(Integer.valueOf(1)) removes the first occurrence of the value 1. Mixing them up gives wrong results with no error.

If you know the expected size, use new ArrayList<>(n) to avoid repeated resizing. And use trimToSize only when memory is really a concern.

Code examples

remove(int) vs remove(Object) and deque usage

import java.util.*;

public class ListDemo {
    public static void main(String[] args) {
        List<Integer> al = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            al.add(i);
        }
        al.remove(Integer.valueOf(3));
        al.remove(0);
        System.out.println(al);

        Deque<String> queue = new ArrayDeque<>();
        queue.offer("first");
        queue.offer("second");
        System.out.println(queue.poll());

        LinkedList<Integer> ll = new LinkedList<>(List.of(1, 2, 3));
        ll.addFirst(0);
        ll.removeLast();
        System.out.println(ll);
    }
}

remove(Integer.valueOf(3)) removes the value 3, then remove(0) removes the element at index 0. ArrayDeque gives FIFO behaviour, and LinkedList supports operations at both ends.

Key points

Common follow-up questions

Interview tips

Concepts covered

ArrayList, LinkedList, Time Complexity, Cache Locality, Deque

More Core Java & Collections questions