FrontendInterviews.dev

Loading problem…

299. Remove Nth Node From End of List

Medium•
Acceptance: 85.71%
•
🔓3/3 Pro unlocks today

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

This classic two-pointer problem requires you to find and remove a node at a specific position from the end, ideally in a single pass.

Requirements

1. Basic Functionality

  • Remove the nth node from the end of the list
  • Return the head of the modified list
  • Handle removing the head node

Example Usage

// List: 1 → 2 → 3 → 4 → 5,  n = 2
// Remove 4 (2nd from end)
// Result: 1 → 2 → 3 → 5

Real-World Context

  • Browser history: Removing a specific entry from recent history
  • Undo stack: Removing a specific action from the undo history
  • Log rotation: Removing entries at a specific offset from the most recent

Constraints

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