Linked listsAnimated
Find Nth Node from End
Start two pointers n apart and move them together.
Time
O(n)
Extra space
O(1)
Use it for
Single-pass "kth from last".
How it works
- Move a lead pointer n steps ahead of a second pointer.
- Then advance both together until the lead falls off the end.
- The second pointer is now exactly n nodes from the end.
- One pass, O(n), without counting the length.
Watch it run
Loading animation…