Delete the element with value X

Pattern: Traversal and dummy nodes

easy

Put a dummy node before the head and walk with prev. When prev.next holds X, skip over it. The dummy makes deleting the head no different from deleting any other node.

The problem

Given the head of a singly linked list and a value X, delete the node holding X and return the head. X appears at most once. If it does not appear, return the list unchanged. For example, 1 → 2 → 3 with X = 2 becomes 1 → 3, and 3 → 4 → 5 with X = 7 stays the same.

The function name deleteNodeWithValueX matches the takeUforward practice page. Use the link above for the exact statement and limits.

Think first

  • When you find X, which node must change its pointer?
  • How do you handle X being in the head?
  • What if X is not there?

Hints

    Brute force, and why it fails

    Handle the head as a special case and then walk the rest: correct but easy to get wrong. The dummy node gives one simple loop. Both are O(n).

    The key insight

    A dummy node gives every real node a predecessor. Then "delete the node after prev" works everywhere, including at the head.

    Dry run

    dummy0prev
    11 
    22 
    33 

    The list is 1 → 2 → 3 and X = 2. A dummy node goes in front, and prev starts on it.

    Step 1 of 4

    The plan

    dummy.next = head, prev = dummy
    while prev.next is not null:
        if prev.next.val == X: prev.next = prev.next.next; stop
        prev = prev.next
    return dummy.next
    

    Complexity

    Time
    O(n)
    Space
    O(1)

    One walk, and one extra node.

    Edge cases

    • X in the head: the second node becomes the head.
    • X in the tail: the second-to-last node ends the list.
    • X absent, or the list empty: unchanged.
    I've built my logic, show the code

    Java

    class Solution {
        public ListNode deleteNodeWithValueX(ListNode head, int X) {
            ListNode dummy = new ListNode(0);       // a node before the head, so the head is not a special case
            dummy.next = head;
            for (ListNode prev = dummy; prev.next != null; prev = prev.next) {
                if (prev.next.val == X) {
                    prev.next = prev.next.next;     // unlink it
                    break;
                }
            }
            return dummy.next;
        }
    }

    C++

    class Solution {
    public:
        ListNode* deleteNodeWithValueX(ListNode* head, int X) {
            ListNode dummy(0);                      // a node before the head, so the head is not a special case
            dummy.next = head;
            for (ListNode* prev = &dummy; prev->next != NULL; prev = prev->next) {
                if (prev->next->val == X) {
                    prev->next = prev->next->next;  // unlink it
                    break;
                }
            }
            return dummy.next;
        }
    };

    Python

    from lib import ListNode
    
    class Solution:
        def deleteNodeWithValueX(self, head: ListNode, X: int) -> ListNode:
            dummy = ListNode(0, head)               # a node before the head, so the head is not a special case
            prev = dummy
            while prev.next is not None:
                if prev.next.val == X:
                    prev.next = prev.next.next      # unlink it
                    break
                prev = prev.next
            return dummy.next

    Common mistakes

    • Checking cur.val instead of prev.next.val, which finds X one node too late.
    • Returning head when the head was deleted.
    • Forgetting to stop after the deletion, when only one copy should go.

    Similar problems