Problem
Section titled “Problem”Given the head of a linked list, remove the node from the end of the list and return its head.
Example 1:

Input: head = [1,2,3,4,5], n = 2Output: [1,2,3,5]Example 2:
Input: head = [1], n = 1Output: []Example 3:
Input: head = [1,2], n = 1Output: [1]Constraints:
- The number of nodes in the list is
sz. -
Node.val
Follow-up: Could you do this in one pass?
Solution approach
Section titled “Solution approach”Intuition
Section titled “Intuition”The direct method counts the length L, then walks again to node L - n. That is two passes; the follow-up asks for one.
To find the n-th node from the end without knowing L, keep two pointers exactly n nodes apart. When the front pointer fast falls off the end of the list, the back pointer slow is n nodes behind the end — which is exactly the node before the one to delete, if we start both from a dummy node placed in front of head.
The dummy node also removes the special case of deleting the head itself: the head simply becomes “the node after the dummy”, and we return dummy.next at the end.
Algorithm
Section titled “Algorithm”- Create a
dummynode whosenextishead. Setfast = headandslow = &dummy. - Advance
fastbynnodes. Nowfastisnnodes ahead ofslow->next. - While
fastis not null, advance bothfastandslowby one node. The gap is preserved. - When
fastbecomes null,slow->nextis then-th node from the end. Save it astarget, unlink it withslow->next = target->next, and free it. - Return
dummy.next.
Walkthrough
Section titled “Walkthrough”D is the dummy node. Blue is slow, orange is the node fast points at; the gap between them never changes:
Complexity
Section titled “Complexity”- Time: —
fastvisits every node once andslowtrails behind it, all in a single pass. - Space: — a dummy node and two pointers.
Edge cases
Section titled “Edge cases”- Removing the head (
n == sz, e.g. Example 2 or[1,2]withn = 2): after step 2fastis already null, soslowstays atdummyanddummy.nextis moved past the head. The result may be an empty list. - Removing the tail (
n = 1, Example 3):slowstops at the second-to-last node and itsnextbecomes null.
Think before you peek
Work out an approach on your own first — the explanation will still be here when you want it.
class Solution { public: ListNode* removeNthFromEnd(ListNode* head, int n) { ListNode dummy(0, head); ListNode* fast = head; ListNode* slow = &dummy; for (int i = 0; i < n; i++) fast = fast->next; while (fast) { fast = fast->next; slow = slow->next; } ListNode* target = slow->next; slow->next = target->next; delete target; return dummy.next; }};class Solution { public ListNode removeNthFromEnd(ListNode head, int n) { ListNode dummy = new ListNode(0, head); ListNode fast = head, slow = dummy; for (int i = 0; i < n; i++) fast = fast.next; while (fast != null) { fast = fast.next; slow = slow.next; } slow.next = slow.next.next; return dummy.next; }}class Solution: def removeNthFromEnd(self, head: ListNode | None, n: int) -> ListNode | None: dummy = ListNode(0, head) fast, slow = head, dummy for _ in range(n): fast = fast.next while fast: fast = fast.next slow = slow.next slow.next = slow.next.next return dummy.nextGive it a try first
Attempt the problem on LeetCode — the code will still be here.







