← All problems

Remove Nth Node From End of List

medium

Given the head of a linked list, remove the nth node from the end and return the (possibly new) head. Do it in one pass.

Send a fast pointer n nodes ahead first, then move both pointers together — when fast runs out, slow sits right before the node to remove.

Example

Input: head = [1, 2, 3, 4, 5], n = 2
Output: [1, 2, 3, 5]
Loading...