Remove Nth Node From End of List

Medium

Description

Given the head of a linked list, remove the nth node from the end of the list and return its head. You should do this in one pass through the list.

Examples

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

Remove the 2nd node from the end (4).

Input:head = [1], n = 1
Output:[]
Explanation:

Remove the only node.

Input:head = [1,2,3], n = 3
Output:[2,3]
Explanation:

Remove the 3rd node from the end, which is the first node (1). This demonstrates removing the head node from a multi-node list.

Constraints

  • The number of nodes in the list is sz.
  • 1 ≤ sz ≤ 30
  • 1 ≤ n ≤ sz

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!