Skip to content
AI360Xpert
Beta
Difficulty: MediumLinked List

Remove Nth Node From End of List

Problem in Plain English

Given the head of a singly linked list and an integer n, remove the nth node from the end of the list in a single pass and return the head of the modified list.

Problem Statement

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

The position n is always valid, and the follow-up asks for a single-pass solution.

Reuse the surviving nodes in their original order and change only the link needed to bypass the target. The removed node need not have its own next link cleared; it simply must be unreachable from the returned head.

Constraints

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

Examples

Example 1

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

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

Example 2

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

Removing the only node leaves an empty list.

Example 3

Input
head = [1,2], n = 1
Output
[1]

The 1st node from the end holds 2, so removing it leaves [1].

Example 4

Input
head = [7,7,7], n = 3
Output
[7,7]

The 3rd node from the end is the original head even though all values are equal. Return the original second node, retaining the other two nodes in order; values alone cannot identify which node was removed.

Intuition

A singly linked list cannot walk backward, so locate the target's predecessor using information gathered while moving forward. If the length is L, the target has one-based position L - n + 1; in Example 1 that is position 4, holding value 4. Either measure L first, or keep a leader n + 1 links ahead of a trailer so the leader reaching null places the trailer immediately before the target. A dummy predecessor makes removing the head work like any other deletion.

Approaches

Two Passes

Solution Details

Reveal Two Passes: intuition, complexity, and code

Hints

Hint 1
Translate the end-relative position into a forward position: with length L, the target is the node at one-based position L - n + 1.
Hint 2
Changing a singly linked list requires reaching the predecessor of the target; a dummy supplies the predecessor when the target is head.
Hint 3
Start both pointers at dummy and move the leader n + 1 links ahead. Preserve that link gap until the leader reaches null.

Edge Cases

  • Removing the head (n equals the length): left never leaves dummy, so the bypass still works.
  • A single-node list with n = 1: the bypass empties the list and dummy.next is null.
  • Removing the tail (n = 1) changes its predecessor.next to null; no surviving node should still reach the tail.
  • Duplicate values do not affect which position is removed; keep survivor identities and relative order.

Common Mistakes and Interview Tips

  • With the shown null-stopping convention, moving right only n links parks left on the target instead of its predecessor; a different stopping condition would be needed.
  • Returning the original head after removing it; return dummy.next in the gap approach or head.next in the two-pass head case.
  • Confusing the requested n with list length L when stating complexity or converting the target index.
  • Searching for a matching value instead of a position removes the wrong node when values repeat.
  • Clearing the target's next link before saving its successor loses the remaining suffix; the shown snippets bypass it without modifying that link.

Key Takeaway

A dummy node absorbs deletions at the head, and a fixed gap between two pointers converts distance-from-the-end into a one-pass location.