Skip to main content

37. Remove Nth Node From End of List

mediumAsked at Workday

Given the head of a linked list, remove the nth node from the end. Workday uses this for two-pointer-with-gap fluency — pointer hygiene mistakes show up immediately.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Workday loops.

  • Glassdoor (2025)Workday SDE2 phone screen.

Problem

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

Constraints

  • The number of nodes in the list is sz.
  • 1 <= sz <= 30
  • 0 <= Node.val <= 100
  • 1 <= n <= sz
  • Follow-up: Could you do this in one pass?

Examples

Example 1

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

Example 2

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

Example 3

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

Approaches

1. Two pass (count then walk)

Count length L; walk to L - n; skip.

Time
O(L)
Space
O(1)
let len = 0; for (let n = head; n; n = n.next) len++;
const dummy = {next: head}; let cur = dummy;
for (let i = 0; i < len - n; i++) cur = cur.next;
cur.next = cur.next.next;
return dummy.next;

Tradeoff: Two passes — fails the follow-up.

2. Two pointers with gap

Advance fast by n. Then move both until fast hits end. Slow is the predecessor of the target.

Time
O(L)
Space
O(1)
function removeNthFromEnd(head, n) {
  const dummy = { val: 0, next: head };
  let slow = dummy, fast = dummy;
  for (let i = 0; i < n + 1; i++) fast = fast.next;
  while (fast) { slow = slow.next; fast = fast.next; }
  slow.next = slow.next.next;
  return dummy.next;
}

Tradeoff: Single pass. Dummy head handles the 'remove first node' case uniformly.

Workday-specific tips

Workday wants the one-pass version. The dummy head is mandatory — without it, removing the first node forces a special case. Advance fast by n+1 (not n) because we want slow to land on the predecessor.

Common mistakes

  • Advancing fast by n instead of n + 1 — slow lands on the target, not the predecessor.
  • No dummy head — removing the first node crashes.
  • Not handling head being the last node correctly.

Follow-up questions

An interviewer at Workday may pivot to one of these next:

  • Find the middle of the list (LC 876).
  • Linked List Cycle II (LC 142).
  • Reorder List (LC 143).

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why n+1 instead of n?

We want slow to be the predecessor of the target. If fast leads by n, slow lands ON the target. n+1 leads ensures slow is just before.

Why a dummy head?

If n equals list length, the target IS the head. Without a dummy, you'd need a special case. The dummy gives slow a real predecessor for every target.

Practice these live with InterviewChamp.AI

Drill Remove Nth Node From End of List and other Workday interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →