medium Remove Nth Node From End of List
Problem Statement
Given a linked list, remove the last nth node from the end of the list and return the head of the modified list.
Example 1:
- Input: list = 1 -> 2 -> 3 -> 4 -> 5, n = 2
- Expected Output: 1 -> 2 -> 3 -> 5
- Justification: The 2nd node from the end is "4", so after removing it, the list becomes [1,2,3,5].
Example 2:
- Input: list = 10 -> 20 -> 30 -> 40, n = 4
- Expected Output: 20 -> 30 -> 40
- Justification: The 4th node from the end is "10", so after removing it, the list becomes [20,30,40].
Example 3:
- Input: list = 7 -> 14 -> 21 -> 28 -> 35, n = 3
- Expected Output: 7 -> 14 -> 28 -> 35
- Justification: The 3rd node from the end is "21", so after removing it, the list becomes [7,14,28,35].
Constraints:
- The number of nodes in the list is
sz. 1 <= sz <= 300 <= Node.val <= 1001 <= n <= sz
Try it yourself
Try solving this question here:
🎯 STRICT STANDOUT — Remove Nth Node From End of List Medium
0. Pattern family
Family: Two pointers linked list (gap of n)
1. Why / judgment (K3)
One pass: advance fast n steps, then move both until fast ends; slow is before target. Dummy head handles remove head.
2. Worked complexity / derivation (K11)
O(L) one pass, O(1) space. Two-pass length-then-remove also O(L).
3. Pattern + recognition + when-NOT (K12)
Name: K-TH FROM END (fast/slow gap)
Recognition: singly linked list; delete nth from end; no random access.
When-NOT: Array → index L-n. Doubly linked with tail → from back. Need node value not position → search.
4. Edge hand-run (K13)
[1,2,3,4,5] n=2 → [1,2,3,5]. n=L remove head. n=1 remove tail. Single node n=1 → empty.
5. Interviewer follow-ups & drills
Q1. Dummy head why?
Model answer: Uniform delete when target is head.
Q2. Off-by-one?
Model answer: Gap of n between pointers; stop when fast at last node so slow.next is target.
Q3. n > length?
Model answer: Constraints usually forbid; else guard.
✅ Solution Remove Nth Node From End of List
Problem Statement
Given a linked list, remove the last nth node from the end of the list and return the head of the modified list.
Example 1:
- Input: list = 1 -> 2 -> 3 -> 4 -> 5, n = 2
- Expected Output: 1 -> 2 -> 3 -> 5
- Justification: The 2nd node from the end is "4", so after removing it, the list becomes [1,2,3,5].
Example 2:
- Input: list = 10 -> 20 -> 30 -> 40, n = 4
- Expected Output: 20 -> 30 -> 40
- Justification: The 4th node from the end is "10", so after removing it, the list becomes [20,30,40].
Example 3:
- Input: list = 7 -> 14 -> 21 -> 28 -> 35, n = 3
- Expected Output: 7 -> 14 -> 28 -> 35
- Justification: The 3rd node from the end is "21", so after removing it, the list becomes [7,14,28,35].
Constraints:
- The number of nodes in the list is
sz. 1 <= sz <= 300 <= Node.val <= 1001 <= n <= sz
Solution
-
Two-Pass Approach:
- Begin by calculating the length of the linked list. This can be done by traversing the list from the head to the end.
- Once the length is determined, calculate which node to remove by subtracting
nfrom the length. - Traverse the list again and remove the node at the calculated position.
-
One-Pass Approach using Two Pointers:
- Use two pointers,
firstandsecond, and place them at the start of the list. - Move the
firstpointernnodes ahead in the list. - Now, move both
firstandsecondpointers one step at a time until thefirstpointer reaches the end of the list. Thesecondpointer will now bennodes from the end. - Remove the node next to the
secondpointer.
- Use two pointers,
-
Advantage of One-Pass Approach:
- The one-pass approach is more efficient as it traverses the list only once, whereas the two-pass approach requires two traversals.
-
Edge Cases:
- If
nis equal to the length of the list, remove the head of the list.
- If
Algorithm Walkthrough
Using the input list = [1,2,3,4,5], n = 2:
- Initialize two pointers,
firstandsecond, at the head of the list. - Move the
firstpointer 2 nodes ahead. Now,firstpoints to "3" andsecondpoints to "1". - Move both
firstandsecondpointers one step at a time. Whenfirstreaches "5",secondwill be at "3". - The next node to
secondis "4", which is the node to be removed. - Remove "4" by updating the next pointer of "3" to point to "5".
- The modified list is [1,2,3,5].
Code
// class ListNode {
// int val = 0;
// ListNode next;
// ListNode(int val) {
// this.val = val;
// next = null;
// }
// }
public class Solution {
public static ListNode removeNth(ListNode head, int n) {
// Create a dummy node to simplify edge cases
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode first = dummy;
ListNode second = dummy;
// Move first pointer n nodes ahead
for (int i = 1; i <= n + 1; i++) {
first = first.next;
}
// Move first to the end, maintaining the gap
while (first != null) {
first = first.next;
second = second.next;
}
// Remove the nth node from the end
second.next = second.next.next;
return dummy.next;
}
public static void main(String[] args) {
ListNode head = new ListNode(1);
head.next = new ListNode(2);
head.next.next = new ListNode(3);
head.next.next.next = new ListNode(4);
head.next.next.next.next = new ListNode(5);
ListNode result = Solution.removeNth(head, 2);
System.out.print("Nodes after removing the Nth node from the end: ");
while (result != null) {
System.out.print(result.val + " ");
result = result.next;
}
}
}
Complexity Analysis
- Time Complexity: O(L) - We traverse the list with two pointers. Here, L is the number of nodes in the list.
- Space Complexity: O(1) - We only used constant extra space.
🎯 STRICT STANDOUT — Solution Remove Nth Node From End of List
0. Pattern family
Family: Two pointers linked list
1. Why / judgment (K3)
Dummy + fast ahead by n; joint march; rewire slow.next. One pass clean API.
2. Worked complexity / derivation (K11)
O(L) time O(1) space.
3. Pattern + recognition + when-NOT (K12)
Name: K-TH FROM END DELETE
Recognition: singly LL remove nth from end.
When-NOT: Need reverse list permanently → reverse twice alternative.
4. Edge hand-run (K13)
Length n remove last; length n remove first via dummy.
5. Interviewer follow-ups & drills
Q1. Loop condition?
Model answer: while fast: advance both; detail variants with fast.next.
Q2. Memory free?
Model answer: Language-dependent; unlink is enough for GC.
Q3. Two-pass trade?
Model answer: Clearer length first; still O(L).
Recognize it: In-place pointer surgery → a dummy head + fast/slow pointers.
▶ Visualize this problem (step it, predict each fork)
🤖 Don't fully get this? Learn it with Claude
Stuck on Remove Nth Node From End of List? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.
Progressively stronger hints — you still solve it.
I'm working on the problem **Remove Nth Node From End of List** (DSA). Give me a HINT LADDER: start with the tiniest nudge, then wait. Only reveal the next, stronger hint when I ask. Do NOT show the full solution unless I type 'show solution'. Keep me doing the thinking. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
See the technique, not just code.
Explain the optimal approach to **Remove Nth Node From End of List** with a VISUAL walkthrough: trace it on a small concrete example using ASCII art / a step-by-step diagram, narrate what changes each step, then give time & space complexity with a one-line derivation. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Catch bugs, edge cases, sub-optimality.
I'll paste my solution to **Remove Nth Node From End of List**. Review it for correctness, missed edge cases, and time/space complexity, then coach me toward the optimal — don't just rewrite it. Ask me to paste my code now. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Lock in recognition with look-alikes.
Give me 2 problems that use the SAME underlying pattern as **Remove Nth Node From End of List**. For each, let me attempt first, then review my answer and name the trigger signal that reveals the pattern. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.