Linked List · LC #206
Reverse Linked List
Reverse a singly linked list in-place using three pointers and return the new head. Classic O(n) time, O(1) space solution.
Statement
Problem & Approach
Problem statement, sample I/O, and key reasoning.
Given the head of a singly linked list, reverse the list, and return the reversed list.
Sample I/O
Input: 1 → 2 → 3 → 4 → 5 Output: 5 → 4 → 3 → 2 → 1 Input: head = [] Output: []
Intuition
How to Think About It
Intuition
A singly linked list can only be traversed forwards, so reversing requires redirecting each node's next pointer to its predecessor. By maintaining three pointers (prev, curr, next) we can flip one pointer per step without losing the rest of the list. When curr reaches null the traversal is done and prev holds the new head.
Core Concept
Initialise prev = null and curr = head. On each iteration: save curr.next, set curr.next = prev, advance prev = curr, advance curr = saved next. Repeat until curr is null. Return prev. This is O(n) time and O(1) space. A recursive variant also exists but uses O(n) stack space.
When to use
Whenever you need to reverse a linked list or reverse a sublist (as a building block for problems like Reorder List or Palindrome Linked List).
When NOT to use
When the list is doubly linked (trivial bidirectional traversal) or when random access is needed (use an array instead).
Key Insights
What to Know Cold
- Save curr.next before overwriting it - otherwise the rest of the list is lost.
- prev starts as null because the original head becomes the tail and must point to null.
- The loop terminates when curr == null; at that point prev is the new head.
- Recursive solution is elegant but risks stack overflow on very long lists.
- Frequently used as a helper in more complex list problems (reverse sublist, palindrome check).
1 example
Worked Examples
Standard 5-node reversal
Reverse 1→2→3→4→5
Iterative three-pointer approach
function reverseList(head: ListNode | null): ListNode | null {
let prev: ListNode | null = null, curr = head;
while (curr) {
const next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
}Output: 5→4→3→2→1
Demonstrates O(1) space in-place pointer manipulation - a prerequisite pattern for Reorder List and many other hard problems.