Challenge - 5 Problems
Circular List Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output after deleting a node with value 3
What is the printed state of the circular linked list after deleting the node with value 3?
DSA Python
class Node: def __init__(self, data): self.data = data self.next = None def print_circular_list(head): if not head: print("Empty") return curr = head result = [] while True: result.append(str(curr.data)) curr = curr.next if curr == head: break print(" -> ".join(result) + " -> null") def delete_node(head, key): if not head: return None curr = head prev = None while True: if curr.data == key: if prev: prev.next = curr.next else: # Deleting head node tail = head while tail.next != head: tail = tail.next if tail == head: return None tail.next = head.next head = head.next return head prev = curr curr = curr.next if curr == head: break return head head = Node(1) head.next = Node(2) head.next.next = Node(3) head.next.next.next = Node(4) head.next.next.next.next = head head = delete_node(head, 3) print_circular_list(head)
Attempts:
2 left
💡 Hint
Remember to update the links correctly when deleting a node in a circular list.
✗ Incorrect
The node with value 3 is removed. The list links 2 directly to 4, maintaining circularity. The printed output shows the list starting from head: 1 -> 2 -> 4 -> null.
❓ Predict Output
intermediate2:00remaining
Output after deleting the head node
What is the printed state of the circular linked list after deleting the node with value 1 (the head)?
DSA Python
class Node: def __init__(self, data): self.data = data self.next = None def print_circular_list(head): if not head: print("Empty") return curr = head result = [] while True: result.append(str(curr.data)) curr = curr.next if curr == head: break print(" -> ".join(result) + " -> null") def delete_node(head, key): if not head: return None curr = head prev = None while True: if curr.data == key: if prev: prev.next = curr.next else: # Deleting head node tail = head while tail.next != head: tail = tail.next if tail == head: return None tail.next = head.next head = head.next return head prev = curr curr = curr.next if curr == head: break return head head = Node(1) head.next = Node(2) head.next.next = Node(3) head.next.next.next = Node(4) head.next.next.next.next = head head = delete_node(head, 1) print_circular_list(head)
Attempts:
2 left
💡 Hint
Deleting the head requires updating the tail's next pointer.
✗ Incorrect
When deleting the head (value 1), the tail node (value 4) is updated to point to the new head (value 2). The list becomes 2 -> 3 -> 4 -> null.
🔧 Debug
advanced2:00remaining
Identify the error in this delete function
What error will this code raise when trying to delete a node from a circular linked list?
DSA Python
def delete_node(head, key): if not head: return None curr = head prev = None while curr.data != key: prev = curr curr = curr.next if prev: prev.next = curr.next else: tail = head while tail.next != head: tail = tail.next tail.next = head.next head = head.next return head head = Node(1) head.next = Node(2) head.next.next = Node(3) head.next.next.next = head head = delete_node(head, 4)
Attempts:
2 left
💡 Hint
Consider what happens if the key is not found in the list.
✗ Incorrect
The while loop condition 'while curr.data != key' never breaks if key is not in the list, causing an infinite loop.
🧠 Conceptual
advanced1:30remaining
Why update tail's next pointer when deleting head?
In a circular linked list, why must we update the tail node's next pointer when deleting the head node?
Attempts:
2 left
💡 Hint
Think about the circular nature of the list and how nodes connect.
✗ Incorrect
In a circular linked list, the last node (tail) points back to the head. When the head changes, the tail's next pointer must update to maintain the circle.
🚀 Application
expert3:00remaining
Resulting list after multiple deletions
Given a circular linked list with nodes 10 -> 20 -> 30 -> 40 -> 50 -> null, what is the printed list after deleting nodes with values 10, 30, and 50 in that order?
DSA Python
class Node: def __init__(self, data): self.data = data self.next = None def print_circular_list(head): if not head: print("Empty") return curr = head result = [] while True: result.append(str(curr.data)) curr = curr.next if curr == head: break print(" -> ".join(result) + " -> null") def delete_node(head, key): if not head: return None curr = head prev = None while True: if curr.data == key: if prev: prev.next = curr.next else: tail = head while tail.next != head: tail = tail.next if tail == head: return None tail.next = head.next head = head.next return head prev = curr curr = curr.next if curr == head: break return head head = Node(10) head.next = Node(20) head.next.next = Node(30) head.next.next.next = Node(40) head.next.next.next.next = Node(50) head.next.next.next.next.next = head head = delete_node(head, 10) head = delete_node(head, 30) head = delete_node(head, 50) print_circular_list(head)
Attempts:
2 left
💡 Hint
Delete nodes one by one and update the head accordingly.
✗ Incorrect
Deleting 10 (head) updates head to 20, deleting 30 removes middle node, deleting 50 removes tail. Remaining nodes are 20 and 40 linked circularly.