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

๐Ÿท๏ธ

Difficulty

Easy

๐Ÿ”—

LeetCode

Problem #206

๐Ÿ—๏ธ

Pattern

Reversal โ€” iterative three-pointer

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
INITIAL: prev=null, currโ†’1โ†’2โ†’3โ†’null prev null curr 1 2 3 null STEP 1: next=2, flip 1.next โ†’ null, advance prev=1, curr=2 null โ† 1 prev ยทยทยท 2 curr 3 STEP 2: next=3, flip 2.next โ†’ 1, advance prev=2, curr=3 null โ† 1 โ† 2 prev ยทยทยท 3 curr STEP 3: next=null, flip 3.next โ†’ 2, advance prev=3, curr=null โ†’ DONE null โ† 1 โ† 2 โ† 3 prev = new head Answer: 3 โ†’ 2 โ†’ 1 โœ“
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

ApproachTimeSpaceTrade-off
RecursiveO(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

CaseInputWhy It Matters
Empty listnullcurr starts null โ†’ loop doesn't execute โ†’ return null.
Single node1 โ†’ nullOne iteration: 1.next=null, prev=1 โ†’ return 1.
Two nodes1 โ†’ 2Test 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.

โ† Back to Linked List problems