Problem Statement

Remove Nth Node From End of List

You are given a linked list. A linked list is a chain of boxes called nodes. Each node holds a value and an arrow that points to the next node. You can only follow the arrows forward, one box at a time. You cannot jump to the end, and you cannot count backward, because there are no arrows pointing back. Your job is to remove the node that sits nth from the end of the chain, then return the start of the list (the head).

easyLinked ListLinked ListTime: O(n) · Space: O(1)

Signals to notice

remove nth node from endsingle pass preferredneed the node before the target

Brute force first

Count length, compute position from start, remove — two passes. It is a fair place to begin because it matches the surface of the question, yet it does not capture the deeper structure that makes the problem simpler.

The key insight

Two pointers n apart: when fast reaches end, slow is at the node before the target — single pass. Instead of recomputing the world every time, you preserve just enough context to let the next decision become obvious.

Trace it on head=[1,2,3,4,5], n=2

init: dummy->1->2->3->4->5; left=dummy, right=node1
advance right n=2: right=node2, then right=node3
loop: left=node1, right=node4
loop: left=node2, right=node5
loop: left=node3, right=null -> stop (right past tail)
unlink: left(node3).next = node5, skipping node4
return dummy.next -> [1,2,3,5]

What must stay true

If fast is n nodes ahead of slow, when fast reaches the end, slow is at position length - n. As long as that statement keeps holding, you can trust the steps built on top of it.

Shape of the loop

dummy -> head; left = dummy; right = head
repeat n times: right = right.next
while right is not null:
    left = left.next; right = right.next
left.next = left.next.next   # skip the nth-from-end node
return dummy.next

Pseudocode only — the full worked solution lives in the Solution tab.

Easy way to go wrong

Forgetting the dummy head — needed when removing the actual head node. When the code becomes mechanical before the idea is clear, small edge cases start breaking the whole story.

Linked List Pattern