Skip to content
StackDecode
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

  1. Move a lead pointer n steps ahead of a second pointer.
  2. Then advance both together until the lead falls off the end.
  3. The second pointer is now exactly n nodes from the end.
  4. One pass, O(n), without counting the length.

Watch it run

Loading animation…