Skip to content

4.9.0 — Linked List: The Pattern

Recognition cue. The input is a linked list. That is the whole cue — these problems are not about choosing an algorithm, they are about pointer discipline. They are asked because manipulating pointers correctly is a clean test of careful thinking.

The four techniques

Almost every linked-list problem is one or two of these, combined.

python
# 1. DUMMY HEAD — removes every special case around the first node
dummy = ListNode(0, head)
tail = dummy
...
return dummy.next

# 2. REVERSE — save the way forward before you overwrite the pointer
prev, curr = None, head
while curr:
    nxt = curr.next
    curr.next = prev
    prev, curr = curr, nxt
return prev                 # NOT curr — curr is None

# 3. SLOW AND FAST — middles, cycles, k-from-the-end
slow = fast = head
while fast and fast.next:
    slow, fast = slow.next, fast.next.next

# 4. HASH MAP OF NODES — when you need old-node-to-new-node, or node identity
old_to_new = {}

The rules that prevent most bugs

Save before you overwrite. The moment you assign curr.next, the old value is gone. Every reversal saves nxt first.

Use a dummy head whenever nodes may be added or removed at the front. It converts "is this the head?" from a branch into nothing at all.

Return prev, not curr, after a reversal loop. The loop ends when curr is null.

Know which middle your slow-fast loop finds. while fast and fast.next gives the second middle on an even-length list; while fast.next and fast.next.next gives the first. Trace a four-node list rather than memorising it.

Cut the list before rewiring halves. If you reverse the second half without severing it from the first, you get a cycle.

Compare nodes by identity, is or ===, never by value.

The eleven problems

#ProblemThe one insight
4.9.1Reverse Linked List ★Save next before overwriting it
4.9.2Merge Two Sorted Lists ★The dummy head; attach the leftover whole
4.9.3Reorder List ★Middle, reverse, weave — three techniques in order
4.9.4Remove Nth Node From End ★Open a gap of n, then move both together
4.9.5Copy List with Random Pointer ★Create all nodes first, wire pointers second
4.9.6Add Two Numberswhile l1 or l2 or carry covers every case
4.9.7Linked List Cycle ★The gap shrinks by exactly one, so it cannot skip zero
4.9.8Find the Duplicate NumberAn array is a linked list if values are indices
4.9.9LRU Cache ★Hash map for lookup, doubly linked list for order
4.9.10Merge K Sorted Lists ★Pair up or use a heap — both O(N \log k)
4.9.11Reverse Nodes in K-GroupSeed prev with the node after the group

★ marks the Blind 75 subset.

What the interviewer will push on

"Why a dummy head?" So operations at the front need no special case. This is the most reusable answer in the chapter.

"Prove the slow and fast pointers must meet." The gap shrinks by exactly one per step and cannot go below zero, so it must hit zero.

"Why must an LRU cache use a doubly linked list?" Unlinking a node in O(1) requires its predecessor.

"Iterative or recursive?" Recursion is shorter for reversal and merging, and it costs O(n) stack. On a 100,000-node list that overflows, so iterative is the production answer.

"How do you handle the head being removed?" Dummy head. If they ask this, they are checking the same thing twice.

One thing to volunteer: draw the pointers before writing code. Every hard problem here is bookkeeping, and the failures come from people who start typing before they have the picture.

Recall

  • Four techniques cover the group: dummy head, reversal, slow and fast, and a map of nodes.
  • Reversal: save next first, and return prev, because curr is null when the loop ends.
  • The dummy head removes every special case for the first node — use it whenever nodes are added or removed at the front.
  • Slow and fast: know which middle your loop condition lands on, by tracing a four-node list.
  • Floyd's cycle detection works because the gap shrinks by exactly one, so it cannot skip past zero.
  • Cut the list before rewiring two halves, or you build a cycle.
  • LRU = hash map for lookup + doubly linked list for order, and the node stores its key so eviction can clean the map.
  • Merging k lists is O(N \log k) by pairing or by heap; one-at-a-time is O(Nk).

Next: 4.9.1 Reverse Linked List — the subroutine half this chapter is built from.