Bird
Raised Fist0
DSA Pythonprogramming

Delete a Node from Circular Linked List in DSA Python

Choose your learning style10 modes available
Mental Model
A circular linked list loops back to the start, so deleting a node means adjusting pointers without breaking the circle.
Analogy: Imagine people sitting in a circle holding hands. Removing one person means the two neighbors hold hands directly, keeping the circle unbroken.
head -> 1 -> 2 -> 3 -> 1 (back to head)
           ↑               ↑
          tail ←────────────
Dry Run Walkthrough
Input: list: 1 -> 2 -> 3 -> (back to 1), delete node with value 2
Goal: Remove node with value 2 and keep the list circular
Step 1: start at head node with value 1
head -> [1] -> 2 -> 3 -> 1 (back to head)
Why: We begin searching from the head to find the node to delete
Step 2: move to next node with value 2
head -> 1 -> [2] -> 3 -> 1 (back to head)
Why: Check if this node is the one to delete
Step 3: found node 2 to delete; link previous node 1 directly to node 3
head -> 1 -> 3 -> 1 (back to head)
Why: By skipping node 2, we remove it and keep the circle intact
Step 4: node 2 is removed; list remains circular
head -> 1 -> 3 -> 1 (back to head)
Why: The circle is maintained by linking node 1 to node 3 and node 3 back to head
Result:
head -> 1 -> 3 -> 1 (back to head)
Annotated Code
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class CircularLinkedList:
    def __init__(self):
        self.head = None

    def append(self, data):
        new_node = Node(data)
        if not self.head:
            self.head = new_node
            new_node.next = new_node  # points to itself
            return
        curr = self.head
        while curr.next != self.head:
            curr = curr.next
        curr.next = new_node
        new_node.next = self.head

    def delete_node(self, key):
        if not self.head:
            return  # empty list

        curr = self.head
        prev = None

        # If head is to be deleted and it's the only node
        if curr.data == key and curr.next == self.head:
            self.head = None
            return

        # If head is to be deleted and list has multiple nodes
        if curr.data == key:
            # find last node to update its next pointer
            while curr.next != self.head:
                curr = curr.next
            curr.next = self.head.next
            self.head = self.head.next
            return

        # For other nodes
        prev = self.head
        curr = self.head.next
        while curr != self.head:
            if curr.data == key:
                prev.next = curr.next
                return
            prev = curr
            curr = curr.next

    def print_list(self):
        if not self.head:
            print("List is empty")
            return
        curr = self.head
        result = []
        while True:
            result.append(str(curr.data))
            curr = curr.next
            if curr == self.head:
                break
        print(" -> ".join(result) + " -> (back to head)")

# Driver code
cll = CircularLinkedList()
cll.append(1)
cll.append(2)
cll.append(3)
print("Original list:")
cll.print_list()
cll.delete_node(2)
print("After deleting node with value 2:")
cll.print_list()
if curr.data == key and curr.next == self.head:
handle deletion when only one node exists which is the head
if curr.data == key:
handle deletion when head node matches key and list has multiple nodes
while curr.next != self.head:
find last node to update its next pointer when deleting head
prev.next = curr.next
bypass the node to delete by linking previous node to next node
OutputSuccess
Original list: 1 -> 2 -> 3 -> (back to head) After deleting node with value 2: 1 -> 3 -> (back to head)
Complexity Analysis
Time: O(n) because in worst case we traverse all nodes once to find the node to delete
Space: O(1) because we only use a few pointers regardless of list size
vs Alternative: Compared to arrays where deletion requires shifting elements O(n), circular linked list deletion is O(n) but does not require shifting, only pointer updates
Edge Cases
empty list
no deletion occurs, list remains empty
DSA Python
if not self.head:
            return  # empty list
single node list where node to delete is head
list becomes empty after deletion
DSA Python
if curr.data == key and curr.next == self.head:
            self.head = None
            return
deleting head node in multi-node list
head pointer moves to next node, last node's next updated
DSA Python
if curr.data == key:
            while curr.next != self.head:
                curr = curr.next
            curr.next = self.head.next
            self.head = self.head.next
            return
deleting node not present
list remains unchanged
DSA Python
while curr != self.head:
            if curr.data == key:
                prev.next = curr.next
                return
When to Use This Pattern
When you see a problem asking to remove an element from a circular structure without breaking the loop, reach for the circular linked list deletion pattern because it carefully updates pointers to maintain the circle.
Common Mistakes
Mistake: Not updating the last node's next pointer when deleting the head node
Fix: Traverse to the last node and update its next pointer to the new head before deleting the old head
Mistake: Not handling the single-node list deletion case
Fix: Check if the node to delete is the only node and set head to None
Mistake: Breaking the loop by setting next pointer to null instead of head
Fix: Always ensure the last node's next points back to head to keep the list circular
Summary
Deletes a node from a circular linked list by adjusting pointers to keep the circle intact.
Use when you need to remove elements from a circular linked list without breaking its circular nature.
The key is to update the previous node's next pointer and, if deleting head, update the last node's next pointer to the new head.

Practice

(1/5)
1. What happens when you delete the head node from a circular linked list with multiple nodes?
easy
A. The head moves to the next node and the list remains circular
B. The list becomes empty
C. The last node is deleted instead
D. The list breaks and becomes linear

Solution

  1. Step 1: Understand the head deletion in circular linked list

    When the head node is deleted, the next node becomes the new head.
  2. Step 2: Maintain circular link

    The last node's next pointer is updated to point to the new head, keeping the list circular.
  3. Final Answer:

    The head moves to the next node and the list remains circular -> Option A
  4. Quick Check:

    Head deletion updates head and keeps circular [OK]
Hint: Deleting head shifts head to next, list stays circular [OK]
Common Mistakes:
  • Assuming list becomes empty after head deletion
  • Not updating last node's next pointer
  • Breaking circular link accidentally
2. Which of the following is the correct syntax to delete a node with value `key` from a circular linked list in Python?
easy
A. def deleteNode(head, key): if head.data == key: # delete head logic
B. def deleteNode(head, key): while head.data != key: head = head.next
C. def deleteNode(head, key): if head is None: return None
D. def deleteNode(head, key): head = None return head

Solution

  1. Step 1: Check for empty list

    Before deleting, check if the list is empty (head is None) to avoid errors.
  2. Step 2: Return None if list is empty

    If head is None, return None as there is nothing to delete.
  3. Final Answer:

    def deleteNode(head, key): if head is None: return None -> Option C
  4. Quick Check:

    Empty list check is essential before deletion [OK]
Hint: Always check if list is empty before deleting [OK]
Common Mistakes:
  • Not checking if head is None before deletion
  • Incorrectly looping without exit condition
  • Setting head to None without condition
3. What will be the output list after deleting node with value 3 from this circular linked list: 1 -> 3 -> 5 -> 7 -> (back to 1)?
medium
A. 1 -> 5 -> 7 -> 3 -> (back to 1)
B. 1 -> 5 -> 7 -> (back to 1)
C. 1 -> 3 -> 5 -> 7 -> (back to 1)
D. 3 -> 5 -> 7 -> (back to 3)

Solution

  1. Step 1: Locate node with value 3

    Traverse the list to find node with value 3 between 1 and 5.
  2. Step 2: Remove node 3 and link 1 to 5

    Update node 1's next pointer to node 5, skipping node 3.
  3. Final Answer:

    1 -> 5 -> 7 -> (back to 1) -> Option B
  4. Quick Check:

    Deleted 3, linked 1 to 5 [OK]
Hint: Skip the node with key, link previous to next [OK]
Common Mistakes:
  • Not updating previous node's next pointer
  • Deleting wrong node or not deleting at all
  • Breaking circular link after deletion
4. Given this code snippet to delete a node from a circular linked list, what is the bug?
def deleteNode(head, key):
    if head is None:
        return None
    curr = head
    while curr.next != head:
        if curr.next.data == key:
            curr.next = curr.next.next
            return head
        curr = curr.next
    return head
medium
A. Does not handle deletion if head node contains the key
B. Infinite loop due to missing break
C. Deletes all nodes instead of one
D. Returns None instead of head

Solution

  1. Step 1: Analyze deletion logic

    The code checks nodes after head but never checks if head itself has the key.
  2. Step 2: Identify missing head deletion handling

    If head contains the key, this code will not delete it, causing incorrect behavior.
  3. Final Answer:

    Does not handle deletion if head node contains the key -> Option A
  4. Quick Check:

    Head node deletion must be handled separately [OK]
Hint: Always check if head node matches key before loop [OK]
Common Mistakes:
  • Ignoring head node in deletion logic
  • Assuming loop covers all nodes including head
  • Not updating head pointer after deletion
5. You have a circular linked list with only one node containing value 10. What should be the list after deleting node with value 10?
hard
A. List with node 10 pointing to None
B. List with node 10 pointing to itself
C. List unchanged
D. Empty list (None)

Solution

  1. Step 1: Identify single node list condition

    When the list has only one node, head.next points to head itself.
  2. Step 2: Delete the only node

    Deleting this node means the list becomes empty, so head should be None.
  3. Final Answer:

    Empty list (None) -> Option D
  4. Quick Check:

    Single node deletion empties the list [OK]
Hint: Deleting sole node results in empty list (None) [OK]
Common Mistakes:
  • Leaving node pointing to itself after deletion
  • Not setting head to None
  • Assuming list still has one node
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard