Remove Nth Node from End

Med
Concept
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Remove Nth Node from End

Approach

Use a dummy node before the head to handle edge cases. Advance the fast pointer n+1 steps ahead so there is a gap of n nodes between fast and slow. Move both until fast reaches null. Slow is now just before the target node, so skip it.

Complexity Analysis

Time
O(n)
Space
O(1)

Pattern

Linked List (Two-Pointer Gap)

Why It Works

By maintaining a fixed gap of n nodes between fast and slow, when fast reaches the end of the list, slow is positioned exactly one node before the nth-from-end node, allowing a simple pointer rewire to remove it.

Updated Feb 2026