‹ DS&A interview · Socratic
DSA · Linked List · #146

Remove Nth Node From End of List

Module 42 · difficulty 3/5·30:00starts on first keystroke

Given the `head` of a singly linked list, remove the `n`-th node from the **end** of the list and return the head of the modified list. Implement `function removeNthFromEnd(head, n)` where `head` is a node `{ val, next }` (or `null`) and `n` is a 1-indexed position counted from the tail. Return the new head (which may be `null` if the list becomes empty).

Examples
  • head = [1,2,3,4,5], n = 2 [1,2,3,5]The 2nd node from the end is 4; it is removed.
  • head = [1], n = 1 []Removing the only node yields an empty list.
  • head = [1,2], n = 2 [2]n equals the list length, so the head is removed.
Constraints
  • · The number of nodes in the list is sz.
  • · 1 <= sz <= 30
  • · 0 <= Node.val <= 100
  • · 1 <= n <= sz
Session phases
A · Clarify
B · Approach
C · Complexity
D · Edges
E · Code
F · Tradeoff
G · Score
Phase A — Clarify
Ask questions about input bounds, types, and edge constraints.
Ask the coach clarifying questions about the problem.
When you've covered this phase, advance to the next.