Web Analytics

๐Ÿ”„ Linked List Reversal

Intermediate ~20 min read โญ Top Interview Question

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

Output
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

  1. Reverse first k nodes - Stop after k iterations
  2. Reverse in groups of k - Reverse k, then next k, etc.
  3. Reverse between positions m and n - Reverse only middle part
  4. Check if palindrome - Reverse half, compare with other half
  5. 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

  1. Use 3 pointers: prev (null), current (head), next (temp)
  2. Loop while current exists:
    • Save next: next_node = current.next
    • Reverse: current.next = prev
    • Move forward: prev = current, current = next_node
  3. 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!

Practice: Try reversing a list on paper. Then code it without looking. Then try the variations!