Skip to content
Decoder

Remove the Nth Node From the End

Medium

Problem

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.

Example 1

Input
1
Output
2 -> 3 -> 4 -> 5, n = 2 → 1 -> 2 -> 3 -> 5

Example 2

Input
1, n = 1
Output
(empty)

Example 3

Input
1
Output
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

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.