◀ THE GRIND — LINKED LIST

Reverse Linked List

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

THE BRIEFING — THE FULL DRILL, IN MY OWN WORDS

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.

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 HINTS — TAKE 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 BOARD — THE 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 SPLITS — TWO PACES, TWO LANGUAGES
grind/reverse-linked-list.pyRACE 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-TIMED — ALL 4 SOLUTIONS RAN GREEN AGAINST SELF-AUTHORED CASES IN CI · JDK 21 · CPYTHON 3.12 · NOTHING PUBLISHES RED