7.2 Remove Nth Node From End of List
7.2.1 Problem Metadata
- Platform: LeetCode
- Problem ID: 19
- Difficulty: Medium
- URL: https://leetcode.com/problems/remove-nth-node-from-end-of-list/
- Tags: Blind 75, NeetCode 150
- Techniques: 2.1">Two Pointers, Linked List
7.2.2 Description
Given the head of a linked list, remove the nth node from the end of the list and return its head.
7.2.3 Examples
Input: head = [1,2,3,4,5], n = 2
Output: [1,2,3,5]
Input: head = [1], n = 1
Output: []
7.2.4 Constraints
- The number of nodes in the list is
sz 1 <= sz <= 300 <= Node.val <= 1001 <= n <= sz
7.2.5 Solution - Two Pointers with Gap
7.2.5.1 Walkthrough
This solution uses a two-pointer technique with a fixed gap to find the n-th node from the end in a single pass.
Core Strategy:
- Create a gap of
n+1nodes between two pointers - When the fast pointer reaches the end, the slow pointer is at the node before the target
- Skip over the target node by updating
slow.next
Why n+1 gap? We need the slow pointer to stop at the node before the one we want to remove, so we can update slow.next to skip the target.
Example: Remove 2nd from end in [1,2,3,4,5]:
Initial: dummy → 1 → 2 → 3 → 4 → 5 → null
first
second
After n+1 (3) moves of first:
dummy → 1 → 2 → 3 → 4 → 5 → null
first
second
Move both until first is null:
dummy → 1 → 2 → 3 → 4 → 5 → null
first (null)
second (at node 3)
Remove: second.next = second.next.next
Result: 1 → 2 → 3 → 5
7.2.5.2 Analysis
- Time Complexity: O(n) - Single pass through the list
- Space Complexity: O(1) - Only pointer variables used
7.2.5.3 Implementation Steps
- Create a dummy node pointing to head (handles edge case of removing head).
- Initialize both
firstandsecondpointers at dummy. - Move
firstforwardn+1times to create the gap. - Move both pointers until
firstreaches null. - Skip the target node:
second.next = second.next.next. - Return
dummy.next.
7.2.5.4 Code - Java
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummy = new ListNode(0, head);
ListNode first = dummy, second = dummy;
for (int i = 0; i <= n; i++) {
first = first.next;
}
while (first != null) {
first = first.next;
second = second.next;
}
second.next = second.next.next;
return dummy.next;
}