DSA Tracker

Problem

Pattern this teaches:Fast and Slow Pointers

Medium

Remove Nth Node From End of List

A medium Linked List interview guide. Task statement, worked examples, intuition, and step-by-step solutions.

Topic
Linked List
Sheets
3
Core for
4 roles

The problem

Remove the nth node from the end of the linked list and return its head.

Example 1

Input
[6,3,8,2,9], n=2
Output
[6,3,8,9]

Example 2

Input
[1], n=1
Output
[]

Example 3

Input
[7,4], n=1
Output
[7]

Constraints

  • 1 <= n <= list.length <= 30
  • -100 <= Node.val <= 100

How to think about it

Updated 2026-09-29

You cannot measure backwards from an unknown endpoint in a singly linked list. Fix a rigid window of length n between two forward-marching pointers; when the leading pointer falls off the edge, the trailing pointer is positioned right where surgery needs to happen.

Approaches, worst first

  1. Two-pass counting

    time O(L) · space O(1)

    Count total length L in a preliminary pass, compute target index L - n, and walk a second time to reach the predecessor node before modifying its next reference. Simple and correct, but visits earlier nodes twice.

  2. One-pass sliding window with dummyWrite this one

    time O(L) · space O(1)

    Position a dummy node before head. Advance a fast pointer n + 1 steps ahead of slow. March both pointers forward in tandem until fast drops off the tail; slow now rests precisely on the predecessor of the doomed node, letting `slow.next = slow.next.next` excise it seamlessly.

Where people lose marks · 3
  • Deleting the head node when n equals the full list length requires updating the returned head reference; without a dummy node prepending head, this requires bespoke edge handling.
  • Advancing fast only n steps instead of n + 1 leaves slow landing directly on the target node instead of its predecessor, where the excision pointer must be rewritten.
  • Assuming list length is strictly greater than n; under `n == list.length`, an unchecked initial step on fast can step through null without a dummy guard.

The theory behind it

Linked List — the ground this problem stands on. All Linked List problems

What Linked List is

A linked list is a chain of separate cargo cars connected by coupling hooks, scattered anywhere across memory rather than sitting in a tidy contiguous row. Each car, called a node, holds a single piece of data and a pointer directing traffic to the address of the next car in line. Because nodes connect only by directional links, jumping straight to the tenth car is impossible without walking past the first nine.

When to reach for it

Choose a linked list when a problem requires frequent insertions and deletions at known positions without shifting whole blocks of surrounding memory. Problems mentioning pointer splicing, reversing subsequences in place, merging sorted streams, or detecting cycles in linear chains strongly point here. It is ideal when total capacity is unpredictable and memory allocation must happen one individual node at a time.

How the pattern works

Think in terms of pointer rewiring before dereferencing. Keep a dummy head node pointing to the start of the list so modifications to the initial item do not require separate edge logic. Always save references to neighboring nodes into temporary variables before cutting or redirecting forward links. When diagnosing loops or locating middle nodes, advance two references simultaneously at differing velocities so traversal completes without supplementary storage.

More Linked List problems

Track in your role's order

Pick your target role and all 370 problems resequence to what that interview actually asks.

Start free

Problem set and role mapping as of .