LeetCode ยท #206
Reverse Linked List Solution
Given the head of a singly linked list, reverse the list and return the new head.
01
Section One ยท Problem
Problem Statement
Given the head of a singly linked list, reverse the list and return the reversed list's head.
Example
Input: 1 โ 2 โ 3 โ 4 โ 5 Output: 5 โ 4 โ 3 โ 2 โ 1
Constraints
โข 0 โค number of nodes โค 5000 โข -5000 โค Node.val โค 5000
02
Section Two ยท Approach 1
Stack / Array โ O(n) extra space
Push all nodes onto a stack, then pop them to build the reversed list. Correct but uses O(n) extra space. The iterative approach reverses pointers in-place with O(1) space.
Problem: O(n) extra space for the stack. We can reverse by simply flipping each node's
next pointer to point backward โ no extra storage needed.
03
Section Three ยท Approach 2
Iterative Three-Pointer โ O(n) time, O(1) space
Maintain three pointers: prev, curr, and next. At each step, save the next node, reverse the current node's pointer to point at prev, then advance both prev and curr forward.
๐ก Mental model: Imagine a conga line. You walk down the line and tell each person to turn around and face the person behind them. You need to remember who was next before you make someone turn (otherwise they lose track of the rest of the line). That's the
next = curr.next save before flipping.
Algorithm
- Step 1:
prev = null,curr = head. - Step 2: While
curr โ null: next = curr.next(save)curr.next = prev(reverse)prev = curr(advance prev)curr = next(advance curr)- Step 3: Return
prev(new head).
๐ฏ This pattern is a building block:
- Reversing a linked list (or a portion of it) appears as a sub-routine in LC 143 (Reorder List), LC 92 (Reverse Linked List II), LC 25 (Reverse Nodes in k-Group), and LC 234 (Palindrome Linked List).
- Master this first.
04
Section Four ยท Trace
Visual Walkthrough
Trace through 1 โ 2 โ 3 โ null:
Three-pointer reversal โ flip one link per step
05
Section Five ยท Implementation
Code โ Java & Python
Java โ Iterative
class Solution {
public ListNode reverseList(ListNode head) {
ListNode prev = null, curr = head;
while (curr != null) {
ListNode next = curr.next; // save
curr.next = prev; // reverse
prev = curr; // advance prev
curr = next; // advance curr
}
return prev;
}
}
Python โ Iterative
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
prev, curr = None, head
while curr:
curr.next, prev, curr = prev, curr, curr.next
return prev
Recursive approach
Java โ Recursive
class Solution {
public ListNode reverseList(ListNode head) {
if (head == null || head.next == null) return head;
ListNode newHead = reverseList(head.next);
head.next.next = head; // reverse the link
head.next = null; // old forward link โ null return newHead;
}
}
Python โ Recursive
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
if not head or not head.next: return head
new_head = self.reverseList(head.next)
head.next.next = head
head.next = None return new_head
06
Section Six ยท Analysis
Complexity Analysis
| Approach | Time | Space | Trade-off |
|---|---|---|---|
| Recursive | O(n) | O(n) | Elegant but O(n) call stack |
| Iterative โ optimal | O(n) | O(1) | Three pointers, in-place reversal |
07
Section Seven ยท Edge Cases
Edge Cases & Pitfalls
| Case | Input | Why It Matters |
|---|---|---|
| Empty list | null | curr starts null โ loop doesn't execute โ return null. |
| Single node | 1 โ null | One iteration: 1.next=null, prev=1 โ return 1. |
| Two nodes | 1 โ 2 | Test minimum swap. Result: 2 โ 1 โ null. |
โ Common Mistake: Forgetting to save
curr.next before overwriting it. After curr.next = prev, the original forward link is lost. Without the next save, you can't advance to the rest of the list โ the traversal breaks.