Home / Patterns / Lesson 12

In-Place Linked List Reversal — The Three-Pointer Dance

Medium ⏱ 5 min read 🧩 Pattern 12 of 16

Reversing a linked list in place is the pointer-manipulation exam. Once the three-pointer dance is muscle memory, a whole family of problems opens up.

The dance: prev / cur / next

function reverseList(head) {
  let prev = null, cur = head;
  while (cur) {
    const next = cur.next;   // 1. save where we're going
    cur.next = prev;         // 2. flip the arrow backwards
    prev = cur;              // 3. advance prev
    cur = next;              // 4. advance cur
  }
  return prev;               // prev is the new head
}

Four moves per node, O(n) time, O(1) space. Draw it — three boxes, arrows flipping one at a time. Every linked-list interview answer starts on paper; candidates who juggle pointers mentally produce cycles.

Solve it with hidden tests

Reverse a Linked List on the judge — arrays auto-convert to nodes, so you practice the real thing.

The variants that build on it

  • Reverse a sublist (positions m to n) — walk to m−1, run the dance n−m times, stitch the ends back. The stitching is where bugs live: keep references to the node before the window and the window's original first node (it becomes the window's tail).
  • Reverse in K-groups — repeatedly: check K nodes exist, reverse them, connect to the previous group. A hard-rated classic that is literally the basic dance in a loop.
  • Palindrome list — find middle (fast/slow), reverse second half, compare halves. Two patterns combined.

Spot it when…

  • "Reverse" + "linked list" + "in place / O(1) space". If extra space were allowed, you'd copy to an array — the constraint IS the pattern signal.

Interview tip: narrate the invariant — "everything before prev is already reversed; everything from cur onward is untouched." Invariant-talk is a senior signal.