Asked at

Remove Nth Node From End Of List

Medium
Verified
Linked ListTwo Pointers~20 min

Given a singly linked list as values head (head first) and an integer n, remove the nth node from the end and return the remaining values.

Try the two-pointer pass: advance a lead pointer n steps, then move both until the lead reaches the end.

The input arrives as a single object { head, n }.

Examples

in{ head: [1, 2, 3, 4, 5], n: 2 }
out[1, 2, 3, 5]

The 2nd node from the end is 4; removing it leaves [1, 2, 3, 5].

in{ head: [1], n: 1 }
out[]

Removing the only node leaves an empty list.

Constraints

  • The number of nodes is in the range [1, 30].
  • 0 ≤ Node.val ≤ 100
  • 1 ≤ n ≤ list length

Get help

🔑

Sign in to solve

Sign in to write, run, and submit your solution — and to pick up where your iOS flow left off.