Back to Practice
#0095
Remove Nth Node From End
MediumDSA25 min20 XP
Problem
Given head of a linked list and n, remove the nth node from the end and return the new head.
Why This Matters
This linked-list pattern teaches dummy nodes and pointer gaps, two ideas that remove many edge-case headaches.
Function Signature
def remove_nth_from_end(head, n):
Examples
Example 1
Input1 -> 2 -> 3 -> 4 -> 5, n = 2
Output1 -> 2 -> 3 -> 5
The second node from the end is 4.
Constraints
- Return the exact structure requested.
- Handle the edge cases mentioned in the prompt.
- Prefer clear logic over clever one-liners.
CodePython
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Core example
Input1 -> 2 -> 3 -> 4 -> 5, n = 2
Expected1 -> 2 -> 3 -> 5
The second node from the end is 4.
Hidden Test Categories
remove headsingle-node listremove tail