Skip to content

visual walkthrough

Remove Nth Node From End of List

MediumTwo Pointers (Gap)Reported at: MetaAmazonMicrosoft+5

A fixed gap between two pointers in one pass.

Solve on LeetCode

The idea

"n-th from the end" is easy once you know the length, but that takes a full pass first.

Instead, start a second pointer n + 1 nodes ahead and move both together. When the front one falls off the end, the back one is right before the node to remove.

Complexity

approachtimespace
Count, then walkO(L)O(1)
Fixed gapO(L)O(1)

More walkthroughs