Deletion from a Circular Linked List

Pattern: Doubly and circular lists

medium

Find the node before the head (the last node), then walk the circle once with prev and cur. When cur holds the key, link prev past it; if it was the head, the head moves on, and if it was the only node, the list becomes empty.

The problem

Given the head of a circular singly linked list and a key, delete the first node (from the head) holding the key and return the head of the circle that is left. If the head is deleted, the next node becomes the head. If the only node is deleted, return null. If the key is absent, return the list unchanged. For example, deleting 8 from the circle 2 5 7 8 10 leaves 2 5 7 10.

The link above is a GeeksforGeeks article. The function name deleteNode and these exact rules are this guide's own. The tests print the circle from the returned head round to just before it.

Think first

  • Deleting needs the node before. What is "before" the head in a circle?
  • How do you walk the circle exactly once?
  • What changes when the key is in the head, or is the only node?

Hints

    Brute force, and why it fails

    Break the circle, delete as in a singly linked list, and close it again: correct but fiddly. Walking the circle with prev handles every case in one loop.

    The key insight

    In a circle every node, including the head, has a node before it. Starting prev on the last node turns "delete the head" into an ordinary deletion plus moving the head pointer.

    Dry run

    20cur
    51 
    72 
    83 
    104prev

    The circle is 2 → 5 → 7 → 8 → 10, back to 2, and the key is 8. prev starts on the last node, 10.

    Step 1 of 4

    The plan

    prev = last node (prev.next == head)
    cur = head
    do:
        if cur.val == key:
            if cur.next == cur: return null
            prev.next = cur.next
            return cur.next if cur == head else head
        prev = cur; cur = cur.next
    while cur != head
    return head
    

    Complexity

    Time
    O(n)
    Space
    O(1)

    At most two laps of the circle.

    Edge cases

    • The key is in the head: the next node becomes the head, and the last node points to it.
    • One node holding the key: empty.
    • Key absent: unchanged.
    I've built my logic, show the code

    Java

    class Solution {
        public ListNode deleteNode(ListNode head, int key) {
            if (head == null) return null;
            ListNode prev = head;
            while (prev.next != head) prev = prev.next;   // the last node, which comes before head
            ListNode cur = head;
            do {
                if (cur.val == key) {
                    if (cur.next == cur) return null;      // the only node: the list becomes empty
                    prev.next = cur.next;                  // unlink cur
                    return cur == head ? cur.next : head;  // deleting the head moves the head on
                }
                prev = cur;
                cur = cur.next;
            } while (cur != head);
            return head;                                   // key not found
        }
    }

    C++

    class Solution {
    public:
        ListNode* deleteNode(ListNode* head, int key) {
            if (head == NULL) return NULL;
            ListNode* prev = head;
            while (prev->next != head) prev = prev->next;   // the last node, which comes before head
            ListNode* cur = head;
            do {
                if (cur->val == key) {
                    if (cur->next == cur) return NULL;      // the only node: the list becomes empty
                    prev->next = cur->next;                 // unlink cur
                    return cur == head ? cur->next : head;  // deleting the head moves the head on
                }
                prev = cur;
                cur = cur->next;
            } while (cur != head);
            return head;                                    // key not found
        }
    };

    Python

    from lib import ListNode
    
    class Solution:
        def deleteNode(self, head: ListNode, key: int) -> ListNode:
            if head is None:
                return None
            prev = head
            while prev.next is not head:            # the last node, which comes before head
                prev = prev.next
            cur = head
            while True:
                if cur.val == key:
                    if cur.next is cur:
                        return None                 # the only node: the list becomes empty
                    prev.next = cur.next            # unlink cur
                    return cur.next if cur is head else head   # deleting the head moves the head on
                prev, cur = cur, cur.next
                if cur is head:
                    return head                     # key not found

    Common mistakes

    • Deleting the head without updating the last node's next.
    • Looping while cur != null, which never ends.
    • Forgetting the single-node case, which would leave a node pointing to itself.

    Similar problems