Problem · Canonical Warm-Up
easyin-place · pointersTime · O(n)Space · O(1)
Reverse Linked List
Problem walkthrough: statement, hints, solution, mistakes
Statement
Given the head of a singly linked list, reverse the list in place and return the new head.
Examples
head = 1 → 2 → 3 → 4 → 5 → null
→ 5 → 4 → 3 → 2 → 1 → null
head = 1 → 2 → null
→ 2 → 1 → null
head = null
→ null
head = 1 → null
→ 1 → null
Constraints
0 ≤ list length ≤ 5000-5000 ≤ Node.val ≤ 5000- Solve in O(1) extra memory if asked.
What this problem is really testing
The simplest possible exam of pointer discipline: do you understand that assigning curr.next = prev destroys the only reference to the original successor unless you save it first? Every linked-list problem you'll see later (cycle detection, merging, reordering) relies on this exact pattern. It's the four-line drill that earns its place by being the substrate of everything that follows.
AD SLOT · 728×90