LeetCode 19 Remove Nth Node From End - Solution

The core idea

Use two pointers with a gap of n nodes between them.

First, advance the "fast" pointer n steps ahead. Then move both pointers together until fast reaches the end. At that point, slow is exactly n nodes behind, which means it's right before the node to remove.

Use a dummy node before the head to handle the edge case where you remove the first node. This way, slow can always point to the node before the target.