🚀 Try the Problem
Practice here:
https://leetcode.com/problems/remove-nth-node-from-end-of-list/
🤔 Let’s Think Differently…
Imagine this list:
You are asked:
👉 Remove the 2nd node from the end
So counting from end:
Final list:
🧠 Problem in Simple Words
You are given:
- Head of a linked list
- A number
n
👉 Remove the nth node from the end
👉 Return the updated list
📦 Constraints
🧩 First Thought (Counting Method)
💡 Idea
- Count total nodes
- Find position from start:
- Traverse again and remove that node
✅ Code (Counting Approach)
⏱️ Complexity
Time Complexity
(two traversals)
Space Complexity
⚠️ Limitation of This Approach
👉 It requires two passes
But the problem asks:
Can you solve it in one pass?
🚀 Optimal Approach: Two Pointer Technique (One Pass)
Now comes the interesting part 🔥
🧠 Core Idea
We use two pointers:
🎯 Trick
👉 Move fast pointer n steps ahead
Then move both pointers together until:
At that moment:
👉 slow will be at the node before the one to remove
📌 Why This Works
Because the gap between fast and slow is always n nodes
So when fast reaches end:
👉 slow is exactly where we need it
🔥 Step-by-Step Visualization
List:
n = 2
Step 1: Move fast 2 steps
Step 2: Move both together
👉 Now slow is at node before target
🧼 Clean and Safe Approach (Using Dummy Node)
Using dummy node avoids edge cases like removing head.
💻 Code (Optimal One Pass Solution)
⏱️ Complexity
Time Complexity
(single pass)
Space Complexity
⚖️ Comparing Approaches
| Approach | Passes | Time | Space | Difficulty |
| Counting | 2 | O(n) | O(1) | Easy |
| Two Pointer | 1 | O(n) | O(1) | Optimal |
❌ Common Mistakes
- Forgetting to handle removing head node
- Not using dummy node
- Off-by-one errors in pointer movement
- Moving fast incorrectly
🔥 Interview Insight
This problem is a classic example of:
Used in many problems like:
- Cycle Detection
- Middle of Linked List
- Palindrome Linked List
🧠 Final Thought
At first, counting feels natural…
But once you learn this trick:
"Create a gap and move together"
👉 You unlock a powerful pattern.
🚀 Conclusion
The Remove Nth Node From End problem is not just about deletion…
It teaches:
- Efficient traversal
- Pointer coordination
- One-pass optimization
👉 Tip: Whenever you see “from end”, think:
"Can I use two pointers with a gap?"
That’s your shortcut to solving these problems like a pro 🚀




