Q&A-1Explain what a linked list actually buys and how it changes a production decision.
Answer
The 60-second answer. A linked list buys exactly one thing: O(1) insertion and removal at a node you already hold a reference to. Everything else it does worse than a dynamic array. So the production question is never "list or array" in the abstract: it is "does my algorithm already hold the node?" If yes (LRU cache, free list, lock-free queue) the list wins. If no, you pay O(n) to find the position and you have bought nothing but cache misses.
What it costs in bytes. Assumptions: 64-bit x86-64; C struct {int32_t v; struct node *next;}; Java on HotSpot with compressed oops.
| Representation | Per element | 1M elements |
|---|---|---|
C int32_t[] | 4 B | 4 MB, one allocation |
| C linked node | 16 B (4 value + 4 pad + 8 pointer) | 16 MB, 1M mallocs |
Java int[] | 4 B | 4 MB |
Java LinkedList<Integer> | ~40 B (24 B Node + 16 B Integer) | ~40 MB, 1M objects for the GC to trace |
What it costs in time. Illustrative, on a server x86-64: a dependent load that misses to DRAM costs ~80–100 ns. Scanning 1M int32 in an array is prefetch-friendly and finishes in roughly 1 ms. Walking 1M scattered nodes is ~1M dependent misses, so ~80–100 ms once the list exceeds L3. Measure on your hardware; the ratio is the point, not the absolute.
// splice: unlink the node after `prev` — O(1), no traversal
struct node *victim = prev->next;
prev->next = victim->next;
free(victim);
// reaching position i — i dependent loads, no ILP, no prefetch
struct node *p = head;
for (int k = 0; k < i; k++) p = p->next;The second loop is why the first one is cheap. Each hop depends on the previous load's result, so the CPU cannot overlap them.
The API trap. In Java, LinkedList.remove(Object) is O(n): it scans. The O(1) removal is only reachable through a ListIterator you already positioned:
ListIterator<String> it = list.listIterator();
while (it.hasNext()) {
if (it.next().equals(target)) { it.remove(); break; } // O(1) once positioned
}So "constant-time removal" is a property of your own node class or of an iterator you hold, not of the collection's public surface. That gap is where the slogan breaks.
Failure modes, and how you see them.
- Accidental O(n²).
for (i = 0; i < n; i++) list.get(i)on aLinkedListis quadratic. It shows up as a p99 that grows with input size in a profiler flame graph, not in unit tests with n = 10. - Iterator invalidation. Mutating the list outside the iterator throws
ConcurrentModificationException: loud, and easy to catch in review. - GC and fragmentation. 1M nodes is 1M objects to trace and 1M separate allocations. In Java this shows as GC pause growth; in C, as allocator fragmentation under long uptime.
- Cache cliff. A list that fits in L2 behaves fine and then degrades sharply past it. Benchmark at production scale, not at 1k elements.
When to actually choose one.
- LRU cache:
HashMapfrom key to node plus an intrusive doubly linked list for move-to-front. Java'sLinkedHashMapwithaccessOrder=trueandremoveEldestEntryis this design, already written. - Free lists and slab allocators: the node is embedded in the object, so there is no per-element allocation at all.
- Lock-free queues: a Michael–Scott queue CASes a single
nextpointer, which an array cannot do without a much heavier scheme. - Kernel-style intrusive lists (
list_head): zero overhead beyond two pointers, because the links live inside the payload.
Everywhere else (a queue, a stack, a growable sequence) use a dynamic array. In Java that means ArrayDeque over LinkedList for queue workloads: less memory, better locality, no per-node allocation. Reach for the linked list when the algorithm hands you the node; otherwise you are paying the pointer-chase tax for a splice you never get to use.
Curated: · Written: · Reviewed:
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.