Remove Nth Node From End of List

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

Example 1
[1] -> [2] -> [3] -> [4] -> [5] -> null
---
[1] -> [2] -> [3] -> [5] -> null
Inputhead = [1,2,3,4,5], n = 2
Output[1,2,3,5]
The 2nd node from the end is the node with value 4, so it is removed.
Example 2
[1] -> null
---
null
Inputhead = [1], n = 1
Output[]
The only node is also the 1st node from the end, so removing it leaves an empty list.

Constraints

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

Asked at 19 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate