๐ Linked List Reversal
Reversing a linked list is THE most common linked list interview question - appearing in 50%+ of interviews at Google, Amazon, Microsoft, Facebook, and Apple. Master the 3-pointer technique for O(1) space complexity!
๐ฏ Interview Alert!
This question appears EVERYWHERE:
- โ Google - Asked frequently
- โ Amazon - Top 10 question
- โ Microsoft - Common first-round
- โ Facebook - Standard question
- โ Apple - Appears regularly
If you learn ONE linked list algorithm, make it this one!
The Problem
Given a linked list, reverse it
Input:
HEAD โ [1] โ [2] โ [3] โ [4] โ null
Output:
HEAD โ [4] โ [3] โ [2] โ [1] โ null
Constraints:
- Must reverse in-place
- O(n) time complexity
- O(1) space complexity (no extra data structures!)
The 3-Pointer Technique
The key insight: We need 3 pointers to reverse the list without losing references!
The Magic Formula
prev = None
current = head
while current:
next_node = current.next # 1. Save next!
current.next = prev # 2. Reverse pointer
prev = current # 3. Move forward
current = next_node
head = prev # Update head
That's it! Just 6 lines to reverse a linked list.
See It in Action
Watch the 3 pointers work together:
Step-by-Step Walkthrough
Let's reverse [1] โ [2] โ [3] โ [4] โ null step by step:
Initial State
prev = null
current = 1
next = ?
HEAD โ [1] โ [2] โ [3] โ [4] โ null
Iteration 1: Reverse node 1
Step 1: Save next (so we don't lose the rest!)
next_node = current.next # next_node = 2
Step 2: Reverse the pointer
current.next = prev # 1 now points to null
null โ [1] [2] โ [3] โ [4] โ null
Step 3: Move pointers forward
prev = current # prev = 1
current = next_node # current = 2
null โ [1] [2] โ [3] โ [4] โ null
โ โ
prev curr
Iteration 2: Reverse node 2
Save next: next_node = 3
Reverse pointer: 2 now points to 1
null โ [1] โ [2] [3] โ [4] โ null
Move forward: prev = 2, current = 3
Iteration 3: Reverse node 3
Save next: next_node = 4
Reverse pointer: 3 now points to 2
null โ [1] โ [2] โ [3] [4] โ null
Move forward: prev = 3, current = 4
Iteration 4: Reverse node 4
Save next: next_node = null
Reverse pointer: 4 now points to 3
null โ [1] โ [2] โ [3] โ [4] null
Move forward: prev = 4, current = null
Final Step: Update HEAD
Loop ends (current is null)
Update head: head = prev (which is 4)
HEAD โ [4] โ [3] โ [2] โ [1] โ null
โ Reversed!
Why 3 Pointers?
๐ก Understanding the Need
- prev: Where current should point to (building reversed list)
- current: The node we're currently reversing
- next: Save the rest of the list before we break the link!
Without next: We'd lose the rest of the list when we reverse current's pointer!
This is the #1 mistake: Forgetting to save next before reversing.
The Code
Click Run to execute your code
Complexity Analysis
| Approach | Time | Space | Notes |
|---|---|---|---|
| Iterative (3-pointer) | O(n) โ | O(1) โโ | Best! Use in interviews |
| Recursive | O(n) โ | O(n) | Call stack overhead |
| Using Stack | O(n) | O(n) | Not optimal |
Common Mistakes
โ Mistake #1: Not Saving Next
# WRONG - loses rest of list!
current.next = prev
current = current.next # current.next is now prev!
# RIGHT - save next first
next_node = current.next
current.next = prev
current = next_node
โ Mistake #2: Forgetting to Update Head
# WRONG - head still points to old first node
while current:
# ... reversal logic ...
# head is still [1], not [4]!
# RIGHT - update head to prev
while current:
# ... reversal logic ...
head = prev # Now head is [4]
โ Mistake #3: Not Handling Edge Cases
# WRONG - crashes on empty list
current = head
while current: # Crashes if head is None!
# RIGHT - check first
if not head or not head.next:
return head # Already reversed or empty
Interview Variations
Common Follow-ups
- Reverse first k nodes - Stop after k iterations
- Reverse in groups of k - Reverse k, then next k, etc.
- Reverse between positions m and n - Reverse only middle part
- Check if palindrome - Reverse half, compare with other half
- Reverse alternate k nodes - Reverse k, skip k, reverse k...
Interview Tips
๐ก How to Ace This Question
- โ Draw it out! Visualize on paper before coding
- โ Explain the 3 pointers - show you understand WHY
- โ Handle edge cases: Empty list, single node, two nodes
- โ Mention O(1) space - this is the key advantage!
- โ Walk through an example - show step-by-step
- โ Compare with recursive - show you know trade-offs
โ What Interviewers Want to See
- Understanding of pointer manipulation
- Ability to track multiple variables
- Awareness of space complexity
- Handling edge cases
- Clear communication while coding
Summary
The 3-Pointer Reversal
- Use 3 pointers: prev (null), current (head), next (temp)
- Loop while current exists:
- Save next: next_node = current.next
- Reverse: current.next = prev
- Move forward: prev = current, current = next_node
- Update head: head = prev
Interview tip: This is THE most common linked list question. Practice until you can code it in your sleep! Draw it out, explain each step, and you'll ace it.
What's Next?
You've mastered reversal! Next: Cycle Detection - another top interview question using Floyd's algorithm!
Enjoying these tutorials?