Skip to content
CodeShare
All problems

Remove Nth Node From End

Delete the node that is n places from the end of a linked list.

MediumLinked listTwo pointers

Given the head of a linked list, remove the nth node from the end of the list and return the head.

Lists are written as arrays: [1, 2, 3] means 1 → 2 → 3.

Examples

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

Constraints

  • The list has between 1 and 30 nodes.
  • 0 <= Node.val <= 100
  • 1 <= n <= the length of the list