Skip to content

Remove Nth Node ​

Remove Nth Node — LeetCode

Remove the n-th node from the end of a linked list and return its head, ideally in one pass.

Approach ​

Two pass - Calculate count, go $count - n$ and delete. One pass -

  1. Move $n$ steps.
  2. Make a dummy that points to head. Start another point from dummy, creating a window of size $n + 1$.
  3. Move both pointers one by one.
  4. When right pointer reaches null, the left pointer is just behind the node to remove.
  5. r.next = r.next.next

Remarks ​