Shubham Sourabhthe room · director's cutplain résumé
Room 04 · The Grind — Linked List

Reverse Linked List

EASY✓ CHIP-TIMEDLC #206 — FULL STATEMENT

The briefingthe full drill, in my own words

The drill: Turn a singly linked list around so every arrow points the other way, and hand back what used to be the tail.

A singly linked list arrives, and the job is to flip its direction entirely — every node that used to point forward now points backward, and the node that used to be the tail becomes the new head.

The values inside the nodes never change; only the wiring between them does, and the list can be empty or hold just one node, in which case there's effectively nothing to flip.

The list comes back headed by what used to be its last node, with every arrow reversed all the way through.

  • list length ranges from zero nodes up to a few thousand
  • node values can be any integer, negative or positive
  • only the pointer direction changes, never the stored values
  • an empty list or single node reverses to itself
EX 01
head = [1, 2, 3, 4, 5]
→ [5, 4, 3, 2, 1]
EX 02
head = []
→ []
empty list
EX 03
head = [7]
→ [7]
single node

The hintstake only what you need

HINT 1 THE NUDGE

An array copy makes this trivial — and misses the point. The pointer version asks you to never lose your grip on the rest of the list.

HINT 2 THE STRUCTURE

At each node you need three handles at once: what is behind you (already reversed), where you stand, and what is ahead (untouched).

HINT 3 ONE STEP FROM THE ANSWER

Walk once: save next, point current back at prev, then shuffle prev and current forward. When current runs off the end, prev is the new head.

Coach's boardthe pattern, step by step

THE POINTER FLIPPATTERN · POINTER FLIPhead = 1 → 2 → 3 → 4 → 5
1
2
3
4
5
STEP 1

List is 1 → 2 → 3 → 4 → 5. prev starts null, curr sits at node 1 — reverse the arrows one at a time.

STEP 1 / 7 · ← → WORK TOO

The splitstwo paces, two languages

grind/reverse-linked-list.py — RACE PACE
LANG ▸
PACE ▸
class Solution:
    def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        prev = None
        curr = head
        while curr:
            nxt = curr.next
            curr.next = prev
            prev = curr
            curr = nxt
        return prev
TIME O(N)SPACE O(1)PYTHON · RACE PACE · 10 LN

✓ CHIP-TIMEDALL 4 SOLUTIONS RAN GREEN AGAINST SELF-AUTHORED CASES IN CI · JDK 21 · CPYTHON 3.12 · NOTHING PUBLISHES RED