Remove Nth Node From End of List

Linked Lists, problem 4 of 8

Remove Nth Node From End of List

Medium

LC #19

two pointersdummy head

Not attempted yet

Given the head of a linked list and an integer n, remove the nth node counted from the end (n = 1 is the last node) and return the head of the list.

Example 1

Input: head = [10,20,30,40,50], n = 2
Output: [10,20,30,50]
The 2nd node from the end is 40.

Example 2

Input: head = [4], n = 1
Output: []

Example 3

Input: head = [4,5], n = 2
Output: [5]

Constraints

  • 1 ≤ number of nodes ≤ 5 · 10⁴
  • 1 ≤ n ≤ number of nodes
  • 0 ≤ Node.val ≤ 10⁵

Can you do it in a single pass?

Python

Loading draft…

Test results

7 tests available

No results yet

Run tests your code against the examples; Submit runs the hidden tests too.

3 examples, 4 hidden

Run examples, then submit all tests.