Remove Nth Node From End of List
Remove the n-th node from the end of the list and return the head — ideally in one pass.
Intuition
Counting from the end is awkward in a singly linked list. Give a fast pointer an n-node head start; when fast reaches the end, slow sits exactly on the node just before the one to remove.
Approach
Counting first means two passes
Position-from-the-end is awkward in a singly linked list because you can only move forward. The obvious fix — measure the length, then walk to length − n — works but takes two passes. We can do it in one by keeping a fixed gap between two pointers.
Build an n-node gap, then move together
Advance a fast pointer n steps first, so fast and slow are exactly n nodes apart. Now move both at the same speed until fast reaches the last node. Because the gap is preserved, slow ends up n nodes from the end — specifically, right before the node we want to remove. We converted 'nth from the end' into 'where fast stops,' which needs no length.
A dummy head handles removing the first node
If the target is the head itself, slow needs to stop 'before the head' — so we start both pointers on a dummy node placed before the head. After the walk, splice the target out with slow.next = slow.next.next and return dummy.next. The dummy makes head-removal and single-node lists work with no special case. One pass, O(L) time, O(1) space.
Solution & live demo
Edge cases
The dummy node lets slow stop before the head, so removing the first node needs no special case.
After fast's head start it is null; slow stays on the dummy and unlinks the only node, returning an empty list.
fast walks to the last node; slow stops one before, splicing out the final node cleanly.