Delete Node in a Linked List

Pattern: Traversal and dummy nodes

medium

You are given the node to delete but not the head, so you cannot reach the node before it. Instead, copy the next node's value into this node and delete the next node.

The problem

You are given one node of a singly linked list, but not the head. The node is not the tail, and all values are different. Delete that node's value from the list: afterwards the list must hold the other values in the same order. For example, in 4 → 5 → 1 → 9, deleting the node 5 leaves 4 → 1 → 9.

The function name deleteNode matches LeetCode. Use the link above for the exact statement and limits. This guide's tests build the list, hand the function the node at a given index, and print the list from the head afterwards.

Think first

  • Normally you delete a node from the node before it. Can you reach that node here?
  • What can you change about the node you are given, and about the node after it?
  • Why must the given node not be the tail?

Hints

    Brute force, and why it fails

    Shift every later value one step left and cut the last node: O(n). Copying one value and skipping one node is O(1).

    The key insight

    When you cannot remove a node, remove its neighbour instead, after giving this node the neighbour's value. The list of values comes out exactly right.

    Dry run

    40 
    51 
    12 
    93 

    The list is 4 → 5 → 1 → 9, and you are given the node holding 5. You cannot see the 4.

    Step 1 of 3

    The plan

    node.val = node.next.val
    node.next = node.next.next
    

    Complexity

    Time
    O(1)
    Space
    O(1)

    One copy and one pointer change.

    Edge cases

    • The node is the head: it works the same.
    • The node is second to last: its next becomes null.
    • The tail can never be given, because it has no next node to copy from.
    I've built my logic, show the code

    Java

    class Solution {
        public void deleteNode(ListNode node) {
            node.val = node.next.val;               // copy the next node's value into this one
            node.next = node.next.next;             // and remove the next node instead
        }
    }

    C++

    class Solution {
    public:
        void deleteNode(ListNode* node) {
            node->val = node->next->val;            // copy the next node's value into this one
            node->next = node->next->next;          // and remove the next node instead
        }
    };

    Python

    from lib import ListNode
    
    class Solution:
        def deleteNode(self, node: ListNode) -> None:
            node.val = node.next.val                # copy the next node's value into this one
            node.next = node.next.next              # and remove the next node instead

    Common mistakes

    • Setting node = node.next, which only changes a local variable.
    • Copying the value but forgetting to skip the next node, which duplicates a value.
    • Trying to use this trick on the tail.

    Similar problems