Delete a Node from Circular Linked List in DSA Python - Time & Space Complexity
We want to understand how long it takes to remove a node from a circular linked list.
Specifically, how the time grows as the list gets bigger.
Analyze the time complexity of the following code snippet.
class Node:
def __init__(self, data):
self.data = data
self.next = None
def delete_node(head, key):
if head is None:
return None
curr = head
prev = None
while True:
if curr.data == key:
if prev:
prev.next = curr.next
else:
# Deleting head node
if head.next == head:
return None
temp = head
while temp.next != head:
temp = temp.next
temp.next = head.next
head = head.next
break
prev = curr
curr = curr.next
if curr == head:
break
return head
This code deletes a node with a given value from a circular linked list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Traversing the circular linked list to find the node to delete.
- How many times: Up to n times, where n is the number of nodes in the list.
As the list grows, the time to find the node grows roughly in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 checks |
| 100 | Up to 100 checks |
| 1000 | Up to 1000 checks |
Pattern observation: The number of steps grows directly with the number of nodes.
Time Complexity: O(n)
This means the time to delete a node grows linearly with the list size.
[X] Wrong: "Deleting a node in a circular linked list always takes constant time because it's linked."
[OK] Correct: You must first find the node, which can take time proportional to the list size.
Understanding this helps you explain how linked lists work and how to handle circular references efficiently.
"What if the list was doubly linked? How would the time complexity change?"
Practice
Solution
Step 1: Understand the head deletion in circular linked list
When the head node is deleted, the next node becomes the new head.Step 2: Maintain circular link
The last node's next pointer is updated to point to the new head, keeping the list circular.Final Answer:
The head moves to the next node and the list remains circular -> Option AQuick Check:
Head deletion updates head and keeps circular [OK]
- Assuming list becomes empty after head deletion
- Not updating last node's next pointer
- Breaking circular link accidentally
Solution
Step 1: Check for empty list
Before deleting, check if the list is empty (head is None) to avoid errors.Step 2: Return None if list is empty
If head is None, return None as there is nothing to delete.Final Answer:
def deleteNode(head, key): if head is None: return None -> Option CQuick Check:
Empty list check is essential before deletion [OK]
- Not checking if head is None before deletion
- Incorrectly looping without exit condition
- Setting head to None without condition
Solution
Step 1: Locate node with value 3
Traverse the list to find node with value 3 between 1 and 5.Step 2: Remove node 3 and link 1 to 5
Update node 1's next pointer to node 5, skipping node 3.Final Answer:
1 -> 5 -> 7 -> (back to 1) -> Option BQuick Check:
Deleted 3, linked 1 to 5 [OK]
- Not updating previous node's next pointer
- Deleting wrong node or not deleting at all
- Breaking circular link after deletion
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 headSolution
Step 1: Analyze deletion logic
The code checks nodes after head but never checks if head itself has the key.Step 2: Identify missing head deletion handling
If head contains the key, this code will not delete it, causing incorrect behavior.Final Answer:
Does not handle deletion if head node contains the key -> Option AQuick Check:
Head node deletion must be handled separately [OK]
- Ignoring head node in deletion logic
- Assuming loop covers all nodes including head
- Not updating head pointer after deletion
Solution
Step 1: Identify single node list condition
When the list has only one node, head.next points to head itself.Step 2: Delete the only node
Deleting this node means the list becomes empty, so head should be None.Final Answer:
Empty list (None) -> Option DQuick Check:
Single node deletion empties the list [OK]
- Leaving node pointing to itself after deletion
- Not setting head to None
- Assuming list still has one node
Ready to prove it?
Pass the test and this pattern moves unknown
