Reverse Linked List (LeetCode 206) – The One Trick That Changes Everything
Reverse Linked List (LeetCode 206) – The One Trick That Changes Everything

Reverse Linked List (LeetCode 206) – The One Trick That Changes Everything

From Confusion to Clarity: Master Linked List Reversal with Intuition, Visualization, and Clean Logic

🚀 Try the Problem

Practice here:

https://leetcode.com/problems/reverse-linked-list/


🤔 Let’s Start With a Simple Question…

What happens if you take this:

1 → 2 → 3 → 4 → 5

…and try to reverse it?

You want:

5 → 4 → 3 → 2 → 1

Sounds easy, right?

But here’s the catch:

👉 You can only move forward in a linked list

👉 There is no backward pointer

So how do we reverse something that only goes one way?


🧠 The Core Problem (In Simple Words)

You are given the head of a linked list.

👉 Reverse the list

👉 Return the new head


📦 Constraints

0 <= number of nodes <= 5000
-5000 <= Node.val <= 5000


🔍 Before Jumping to Code — Think Like This

When solving this, your brain might go:

  1. Can I store values somewhere and reverse? ❌ (extra space)
  2. Can I rebuild the list? ❌ (unnecessary)
  3. Can I just change links? ✅ (YES, THIS IS THE KEY)


⚡ The Breakthrough Idea

👉 Instead of moving nodes

👉 We reverse the direction of pointers


🎯 Visual Intuition (Very Important)

Let’s take:

1 → 2 → 3 → 4 → null

We want:

null ← 1 ← 2 ← 3 ← 4

But how?


🧩 The 3-Pointer Magic Trick

We use 3 pointers:

prev → stores previous node
curr → current node
next → stores next node


🔄 Step-by-Step Transformation

Initial State:

prev = null
curr = 1 → 2 → 3 → 4

Step 1:

  1. Save next node
  2. Reverse link
f = 2
1 → null

Move pointers:

prev = 1
curr = 2

Step 2:

f = 3
2 → 1

Move:

prev = 2
curr = 3

Continue…

Eventually:

4 → 3 → 2 → 1 → null


💡 Final Insight

👉 Each step reverses one link

👉 At the end, prev becomes the new head


✅ Clean Java Code (Iterative Approach)

class Solution {
public ListNode reverseList(ListNode head) {

// Previous pointer starts as null
ListNode prev = null;

// Current pointer starts from head
ListNode curr = head;

while (curr != null) {

// Store next node
ListNode f = curr.next;

// Reverse the link
curr.next = prev;

// Move prev forward
prev = curr;

// Move curr forward
curr = f;
}

// New head is prev
return prev;
}
}


⏱️ Complexity

Time Complexity

O(n)

We traverse the list once.

Space Complexity

O(1)

No extra space used.


🔁 Another Way: Recursive Approach

Now let’s think differently…

👉 What if we reverse the rest of the list first, then fix the current node?

🧠 Recursive Idea

For:

1 → 2 → 3 → 4

We:

  1. Reverse from 2 → 3 → 4
  2. Then fix 1


💻 Recursive Code

class Solution {
public ListNode reverseList(ListNode head) {

// Base case
if(head == null || head.next == null)
return head;

// Reverse rest
ListNode newHead = reverseList(head.next);

// Fix current node
head.next.next = head;
head.next = null;

return newHead;
}
}


⚖️ Iterative vs Recursive

ApproachProsCons
IterativeFast, O(1) spaceSlightly tricky to understand
RecursiveElegant, clean logicUses stack (O(n) space)


❌ Common Mistakes

  1. Forgetting to store next node
  2. Losing reference to rest of list
  3. Not updating pointers correctly
  4. Returning wrong node


🔥 Real Interview Insight

This problem is not just about reversing a list.

It teaches:

👉 Pointer manipulation

👉 In-place updates

👉 Thinking in steps

👉 Breaking problems into small operations


🧠 Final Thought

At first, this problem feels tricky…

But once you understand this line:

curr.next = prev

👉 Everything clicks.


🚀 Conclusion

The Reverse Linked List problem is one of the most important foundational problems in DSA.

Mastering it will:

  1. Boost your confidence
  2. Strengthen pointer logic
  3. Help in many advanced problems
👉 Tip: Practice this until you can visualize pointer movement in your head — that’s when you truly master linked lists.

Ai Assistant Kas