Remove Nth Node From End of List
The problem
Section titled “The problem”You get the head of a linked list and a number n. Remove the n-th node counting from
the end (n = 1 is the last node) and return the head.
[1, 2, 3, 4, 5], n = 2 -> [1, 2, 3, 5] (removes 4)Key insight
Section titled “Key insight”The obvious method takes two passes: count the length L, then walk to node L - n.
You can do it in one pass by making the list measure itself.
Use two pointers with a fixed gap of n nodes. Move fast ahead n steps first,
then move both together. When fast reaches the last node, slow is exactly one node
before the one to delete, so slow.next = slow.next.next removes it. Start both at a
dummy head in front of the list. Then slow can stop before the real head, and
removing the first node needs no special case.
Show solutionHide solution
def remove_nth_from_end(head: Optional[ListNode], n: int) -> Optional[ListNode]: dummy = ListNode(0, head) # so removing the head is not a special case fast = slow = dummy for _ in range(n): fast = fast.next # open a gap of n nodes while fast.next: fast = fast.next slow = slow.next # slow stops just before the target slow.next = slow.next.next return dummy.nextexport function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null { const dummy = new ListNode(0, head); // so removing the head is not a special case let fast = dummy; let slow = dummy; for (let i = 0; i < n; i++) fast = fast.next!; // open a gap of n nodes while (fast.next) { fast = fast.next; slow = slow.next!; // slow stops just before the target } slow.next = slow.next!.next; return dummy.next;}class Solution { public ListNode removeNthFromEnd(ListNode head, int n) { ListNode dummy = new ListNode(0, head); // so removing the head is not a special case ListNode fast = dummy, slow = dummy; for (int i = 0; i < n; i++) fast = fast.next; // open a gap of n nodes while (fast.next != null) { fast = fast.next; slow = slow.next; // slow stops just before the target } slow.next = slow.next.next; return dummy.next; }}Complexity
Section titled “Complexity”- Time: O(n) —
fastwalks the list once;slowfollows behind. - Space: O(1) — two pointers and a dummy node.
Watch out for
Section titled “Watch out for”- Removing the head (
nequals the length). Without a dummy node,slowhas nothing to stand on. With it,dummy.nextbecomes the new head. - Stopping condition off by one. Loop while
fast.nextexists so thatslowends before the target, not on it. You need the previous node to relink. - Return
dummy.next, never the originalhead, which may have just been removed. - Single node list with
n = 1should return an empty list (None).
Variations
Section titled “Variations”Remove Duplicates from Sorted List (LeetCode 83)
Section titled “Remove Duplicates from Sorted List (LeetCode 83)”Delete nodes so each value appears once. The trick: walk with one pointer; while the
next node has the same value, skip it with cur.next = cur.next.next, otherwise advance.
Swap Nodes in Pairs (LeetCode 24)
Section titled “Swap Nodes in Pairs (LeetCode 24)”Swap every two adjacent nodes by relinking, not by changing values. The trick: a
dummy head and a prev pointer. For each pair, rewire prev -> second -> first -> rest,
then move prev to first.
Rotate List (LeetCode 61)
Section titled “Rotate List (LeetCode 61)”Rotate the list right by k places. The trick: find the length and tail, reduce k
modulo the length, link the tail to the head to make a ring, then break it at the right
spot: the new head is length - k steps from the old head, so cut just before it.