Back

Remove Nth Node From End of List

medium

Given the head of a linked list, remove the n-th node from the end of the list and return its head.

Test Cases

Copy an input into the main harness and Run to verify
Input
head = [1,2,3,4,5], n = 2
Expected Output
[1,2,3,5]
Input
head = [1], n = 1
Expected Output
[]
Input
head = [1,2], n = 1
Expected Output
[1]

Constraints

  • The number of nodes in the list is sz.
  • 1 <= sz <= 30
  • 0 <= Node.val <= 100
  • 1 <= n <= sz

Hints

Hint 1 — click to reveal

Two pointers separated by n nodes turn 'from the end' into 'from the start'.

Hint 2 — click to reveal

A dummy node handles deleting the head cleanly.

Java Compiler

Powered by OneCompiler. Starter code loads automatically — edit and hit Run.