Remove Nth Node From End: The Classic Gap Technique
Remove Nth Node From End of List (#19) is one of the most elegant linked list problems on LeetCode. It asks a deceptively simple question: given a linked list, remove the nth node from the end and return the head. The trick is doing it in one pass without knowing the list length in advance.
The key insight behind the leetcode 19 solution is the gap technique. If you advance one pointer N steps ahead of another, then move both pointers at the same speed, the trailing pointer will land exactly at the node before the target when the leading pointer reaches the end. This two pointer linked list approach is the foundation for many linked list problems you will encounter in interviews.
In this walkthrough, we will break down the problem, build the one pass nth from end solution step by step, trace through visual examples, and cover every edge case that trips up candidates.
Understanding the Remove Nth From End Problem
You are given the head of a linked list. Remove the nth node from the end of the list and return its head. The value of n is always valid, meaning it is between 1 and the length of the list inclusive.
For example, given the list [1, 2, 3, 4, 5] and n = 2, you need to remove the second node from the end, which is node 4. The result is [1, 2, 3, 5]. The remove nth from end operation must handle cases where the head itself needs to be removed.
A naive approach would traverse the list once to count the length, then traverse again to find the (length - n)th node. That works, but interviewers specifically look for the one pass nth from end solution. The two pointer gap technique gives you exactly that — a single traversal that finds the right position without counting.
Interview Insight
Remove Nth Node From End (#19) is one of the most-asked Medium linked list problems — the one-pass two-pointer technique is what interviewers specifically look for.
The Two Pointer Gap Technique for Remove Nth Node
The core idea is simple: create a gap of exactly N nodes between two pointers. Advance the fast pointer N steps into the list. Then move both fast and slow forward one step at a time. When fast reaches the last node (or null, depending on your setup), slow is sitting right before the node you need to remove.
Why does this work? Think of it like two people walking along a hallway. If person A starts N steps ahead of person B, they will always be N steps apart. When person A reaches the end of the hallway, person B is N steps from the end. That is exactly the position you need — the node before the one to delete.
The remove nth explained simply: advance fast N steps, then walk both until fast hits the end. Set slow.next = slow.next.next to skip over the target node. The fast slow pointer nth technique gives you O(n) time and O(1) space, which is optimal for this problem.
The Dummy Node Trick
There is one tricky edge case with the gap technique: what if you need to remove the head node itself? If n equals the list length, the fast pointer reaches the end during the initial N-step advance, and slow never moves from the start. Without special handling, you have no way to remove the head cleanly.
The solution is a dummy node (sometimes called a sentinel). Create a new node whose next pointer points to the head. Start both fast and slow at this dummy node. Now, even if you need to remove the original head, slow will be sitting at the dummy node and slow.next = slow.next.next correctly skips the head. At the end, return dummy.next as the new head.
This pattern eliminates all special-case logic. Without it, you need an if-statement to check whether you are removing the head, which makes the code messier and more error-prone. The dummy node approach works for every case uniformly.
Pro Tip
Always use a dummy node — it handles the edge case where you need to remove the head itself. Without it, you need special-case logic that makes the code messy.
Visual Walkthrough of Remove Nth Node From End
Let us trace the algorithm on [1, 2, 3, 4, 5] with n = 2. We create a dummy node pointing to 1. Both fast and slow start at dummy.
Step 1 — Advance fast by N = 2 steps: fast moves to node 1, then to node 2. The gap is now 2 nodes. Slow is still at dummy.
Step 2 — Move both until fast.next is null: fast goes to 3, slow goes to 1. Fast goes to 4, slow goes to 2. Fast goes to 5, slow goes to 3. Now fast.next is null, so we stop. Slow is at node 3, which is the node BEFORE node 4 (the one we want to remove).
Step 3 — Delete: slow.next = slow.next.next, which skips node 4. The list becomes [1, 2, 3, 5]. Return dummy.next, which is node 1.
Now consider [1] with n = 1. Dummy points to 1. Advance fast by 1 step: fast goes to node 1. Move both until fast.next is null — fast.next is already null, so we stop immediately. Slow is at dummy. Delete: slow.next = slow.next.next = null. The list is now empty. Return dummy.next = null.
Edge Cases for Remove Nth From End
The first critical edge case is removing the head node. When n equals the length of the list, the fast pointer reaches the end during the initial advance. With a dummy node, slow stays at the dummy, and slow.next = slow.next.next correctly removes the head. Without a dummy node, you need to return head.next as a special case.
The second edge case is a single-node list with n = 1. You need to remove the only node and return an empty list (null). The dummy node handles this gracefully — after deletion, dummy.next is null.
Other cases to consider: n = 1 means remove the last node (the tail), which the algorithm handles naturally because fast reaches the end and slow lands on the second-to-last node. The problem guarantees n is always valid, so you do not need to handle n > list length or n <= 0.
- Remove head: n = list length — dummy node handles it cleanly
- Single node, n = 1: remove only node, return null
- Remove tail: n = 1 — slow lands on second-to-last node
- n equals list length: fast reaches end during initial advance
Common Mistake
The gap technique finds the node BEFORE the one to remove — you need slow.next = slow.next.next to skip the target. If slow IS the target, you've gone one step too far.
What Remove Nth Node From End Teaches You
LeetCode 19 teaches three fundamental linked list skills. First, the gap technique — maintaining a fixed distance between two pointers to locate a position relative to the end of the list. This same idea appears in problems like finding the kth node from the end and detecting where a cycle begins.
Second, the dummy node pattern for clean head removal. Any time a linked list operation might change the head, adding a dummy node at the front eliminates special cases. You will use this in Merge Two Sorted Lists (#21), Remove Duplicates from Sorted List (#83), and Partition List (#86).
Third, the one-pass mindset. Many linked list problems have two-pass solutions that work fine, but the one-pass approach forces you to think about what information you can gather in a single traversal. This skill transfers to stream processing and real-time systems where you cannot go back and re-read data.
Practice the remove nth node from end leetcode problem until the gap setup and dummy node pattern are automatic. YeetCode flashcards help you drill pattern recognition so you immediately see "nth from end" and think "advance one pointer N steps ahead." That instant recognition is what separates confident candidates from those who struggle under time pressure.