Practice

Remove Nth Node From End

Module 7 · Linked Lists

Problem

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

Examples

Example 1

Inputhead = [1,2,3,4,5], n = 2Output[1,2,3,5]

Explanation. removed 4

Example 2

Inputhead = [1], n = 1Output[]

Explanation. removed the only node — the head

Example 3

Inputhead = [1,2], n = 2Output[2]

Explanation. removed the head

Constraints

1 ≤ length ≤ 30 · 1 ≤ n ≤ length · follow-up: one pass.

Attempt it first

The module's graduation exercise: it composes the gap runner (surgery pattern 3), the dummy node (pattern 1), and the stand-on-the-predecessor discipline — all in ~10 lines. The examples deliberately include head-removal; let that steer your setup.