Remove Nth Node From End of List Visualizer & Step-by-Step Algorithm Solution

Remove the n-th node from the end of the list and return its head using a one-pass two-pointer approach with a dummy node.

Category: linked-list | Difficulty: Medium

Tags: Linked List, Two Pointers, Dummy Node

Remove Nth Node From End of List

slow, fast
D
1
2
3
4
5
100%
state
n2
slowDummy
fastDummy
Initialization
1/17
Explanation

Create a dummy node pointing to head Node(1) to handle edge cases like removing the head.

Source Code
1function removeNthFromEnd(head, n) {
2 const dummy = new ListNode(0, head);
3 let fast = dummy;
4 let slow = dummy;
5 for (let i = 0; i <= n; i++) {
6 fast = fast.next;
7 }
8 while (fast) {
9 slow = slow.next;
10 fast = fast.next;
11 }
12 slow.next = slow.next.next;
13 return dummy.next;
14}