Skip to content

Remove the Nth Node From the End

MediumLinked List
Decode it first. Clues, constraints, pattern and plan before any code.

Given the head of a linked list and a number n, remove the nth node counting from the end (n = 1 is the last node) and return the head of the resulting list. Try to do it in one pass.

1 -> 2 -> 3 -> 4 -> 5, n = 2   →  1 -> 2 -> 3 -> 5
1, n = 1                       →  (empty)
1 -> 2, n = 2                  →  2

If nothing is left, return None (null in TypeScript).

Constraints

  • 1 <= number of nodes <= 30
  • 1 <= n <= number of nodes
  • 0 <= Node.val <= 100