Notice Period

Remove the Nth Node From the End

medium

A media player keeps a "recently played" chain, newest first, and the product team wants a feature: drop the track that is n-th from the end — in one pass over the chain, because it streams from disk and re-walking is expensive.

Given the head of a linked list and an integer n, remove the n-th node from the end and return the head of the resulting list. Implement the core in _remove_nth(head, n) on real nodes.

Example 1
in values = [1, 2, 3, 4, 5], n = 2
out [1, 2, 3, 5]
The 2nd node from the end is 4.
Example 2
in values = [1], n = 1
out []
Removing the only node leaves an empty list.
Example 3
in values = [1, 2], n = 1
out [1]
Constraints
  • · 1 <= number of nodes <= 30
  • · 0 <= node value <= 100
  • · 1 <= n <= number of nodes
  • · one pass preferred
similar problem on LeetCode ↗
solution.pyloading python…
test results
Hit ▶ Run to test your code against the visible cases.