Remove the Nth Node From the End

You are given the head of a linked list and a number n. Remove the n-th node from the end of the list (the last node is the 1st from the end) and return the head of the list that is left.

Lists in the examples are written as their values from head to tail: [1,2,3] is 1 → 2 → 3, and [] is an empty list, with no head at all.

Constraints

  • 1 ≤ number of nodes ≤ 5,000
  • 0 ≤ node value ≤ 100
  • 1 ≤ n ≤ number of nodes

Example 1

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

Counting from the end, 5 is 1st and 4 is 2nd, so 4 goes.

Example 2

Input
head = [1]
n = 1
Output
[]
Why:

Removing the only node leaves an empty list.

Example 3

Input
head = [1,2]
n = 1
Output
[1]
Why:

The last node is the 1st from the end.

Hints

    Was this a good challenge?

    Solved by
    0
    Submissions
    0
    Accepted
    –