Skip to main content
23

Remove Nth Node From End of List

Mediumlinked listtwo pointers

Your code isn't saved yet: refreshing or leaving the page resets it. Saved progress comes with user accounts.

This problem uses:Linked ListRead the beginner guide

Given the head of a linked list, remove the n-th node from the end and return the head of the resulting list.

Understand the problem

  • Count from the back: n = 1 is the last node, n = 2 is the second-to-last, and so on.
  • Removing a node means the node before it should skip over it and point to the node after it.
  • n is always valid (between 1 and the length), so the node always exists.
  • If the removed node is the head, the new head is the second node.

Example 1

text
Input:  head = [1, 2, 3, 4, 5], n = 2
Output: [1, 2, 3, 5]

Counting from the end: 5 is 1st, 4 is 2nd. Remove 4.

Example 2

text
Input:  head = [1], n = 1
Output: []

The only node is also the last one. Removing it leaves an empty list.

Example 3

text
Input:  head = [1, 2], n = 2
Output: [2]

The 2nd from the end is the head itself, so the new head is 2.

Constraints

  • 1 <= number of nodes (sz) <= 30
  • 1 <= n <= sz

Note

Follow-up: counting the length first takes two passes. Can you do it in one?

Hints

Stuck? Open one hint at a time. Each one gives away a little more than the last.

    The idea in one sentence

    Move a "fast" pointer n steps ahead, then move it and a "slow" pointer together until fast hits the end: slow is now right before the node to delete.

    The simple approach (two passes)

    1. Walk the list to count its length L.
    2. The node to remove is at position L - n from the start (counting from 0).
    3. Walk again to position L - n - 1 (the node before it) and skip over the target.

    This is correct and O(L) time. It just walks the list twice. The follow-up asks for one pass.

    The key insight

    Use two pointers with a fixed gap:

    • If fast is n nodes ahead of slow, and they move together one step at a time, the gap stays n.
    • When fast reaches the last node, slow is n nodes behind it: right before the n-th from the end.

    To delete a node, you need the node before it, which is exactly where slow ends up.

    Why a dummy node? If we have to remove the head, there's no node before it. A fake dummy node placed in front of head gives every real node a "before", including the head. Both pointers start at dummy, and we return dummy.next at the end.

    Step by step

    dummy → 1 → 2 → 3 → 4 → 5, n = 2:

    1. Move fast ahead n = 2 steps: fast goes dummy → 1 → 2.

    2. Move both until fast.next is null:

    slowfastfast.next
    dummy23
    134
    245
    35null → stop

    3. Skip the next node: slow is 3, so slow.next is 4, the 2nd from the end. Set 3.next = 5.

    Result: 1 → 2 → 3 → 5 ✅

    The code

    The reference solution is below, with comments on every important line.

    How fast is it?

    • Time: O(L). One pass: fast walks the whole list once, and slow follows.
    • Memory: O(1). Two pointers and a dummy node.

    Common mistakes

    • No dummy node. Then removing the head (n equals the length) needs a special case, and forgetting it crashes or returns the wrong head.
    • An off-by-one gap. Starting both at head instead of dummy, or stopping at fast === null instead of fast.next === null, leaves slow on the node to delete instead of the one before it.
    • Returning head. If the head was removed, head is gone. Return dummy.next.

    Going deeper

    The "fixed gap" trick is a cousin of the fast/slow pointers from Linked List Cycle: there, the pointers move at different speeds; here, they move at the same speed with a head start. Both let you find a position in a linked list without knowing its length.

    Reference solution

    js
    /**
     * @param {ListNode} head
     * @param {number} n
     * @return {ListNode | null}
     */
    export default function removeNthFromEnd(head, n) {
      // A fake node before head, so even the head has a node "before" it.
      const dummy = new ListNode(0, head)
      let slow = dummy
      let fast = dummy
      // Give fast a head start of n nodes.
      for (let i = 0; i < n; i++) fast = fast.next
      // Move both until fast is on the LAST node. The gap stays n,
      // so slow ends up right BEFORE the n-th node from the end.
      while (fast.next) {
        slow = slow.next
        fast = fast.next
      }
      // Skip over the target node.
      slow.next = slow.next.next
      return dummy.next // not head: the head itself may have been removed
    }
    ts
    export default function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null {
      // A fake node before head, so even the head has a node "before" it.
      const dummy = new ListNode(0, head)
      let slow = dummy
      let fast = dummy
      // Give fast a head start of n nodes.
      for (let i = 0; i < n; i++) fast = fast.next!
      // Move both until fast is on the LAST node. The gap stays n,
      // so slow ends up right BEFORE the n-th node from the end.
      while (fast.next) {
        slow = slow.next!
        fast = fast.next
      }
      // Skip over the target node.
      slow.next = slow.next!.next
      return dummy.next // not head: the head itself may have been removed
    }
    python
    def remove_nth_from_end(head: ListNode | None, n: int) -> ListNode | None:
        # A fake node before head, so even the head has a node "before" it.
        dummy = ListNode(0, head)
        slow = fast = dummy
        # Give fast a head start of n nodes.
        for _ in range(n):
            fast = fast.next
        # Move both until fast is on the LAST node. The gap stays n,
        # so slow ends up right BEFORE the n-th node from the end.
        while fast.next:
            slow = slow.next
            fast = fast.next
        # Skip over the target node.
        slow.next = slow.next.next
        return dummy.next  # not head: the head itself may have been removed