Challenge - 5 Problems
Circular Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Circular Linked List Traversal
What is the printed output of the following code that traverses a circular linked list starting from the head node?
DSA Python
class Node: def __init__(self, data): self.data = data self.next = None # Create nodes head = Node(10) second = Node(20) third = Node(30) # Link nodes in circular fashion head.next = second second.next = third third.next = head # Traverse circular linked list current = head output = [] while True: output.append(str(current.data)) current = current.next if current == head: break print(' -> '.join(output) + ' -> null')
Attempts:
2 left
💡 Hint
Remember that traversal stops when we reach the head node again.
✗ Incorrect
The traversal starts at head (10), moves to 20, then 30, and stops before repeating head again. So output is '10 -> 20 -> 30 -> null'.
🧠 Conceptual
intermediate1:30remaining
Number of Nodes in Circular Linked List
Given a circular linked list where the head node points back to itself after the last node, how many nodes will be counted by traversing until you reach the head again?
Attempts:
2 left
💡 Hint
Traversal stops when you reach the head again, so count all unique nodes once.
✗ Incorrect
Traversal counts each node once until it reaches the head again, so the count equals the total number of nodes.
🔧 Debug
advanced2:00remaining
Identify the Bug in Circular Linked List Traversal
What error will occur when running this traversal code on a circular linked list?
DSA Python
current = head while current.next != head: print(current.data, end=' -> ') current = current.next print(current.data, end=' -> null')
Attempts:
2 left
💡 Hint
Check if the last node's data is printed or skipped.
✗ Incorrect
The while loop runs while current.next != head, printing data for all nodes except the last inside the loop, then prints the last node's data (' -> null') after the loop. All nodes are printed correctly with no error.
📝 Syntax
advanced1:30remaining
Identify Syntax Error in Circular Linked List Traversal
Which option contains a syntax error when trying to traverse a circular linked list?
DSA Python
current = head while True print(current.data) current = current.next if current == head: break
Attempts:
2 left
💡 Hint
Check the syntax of the while loop line.
✗ Incorrect
The while loop line is missing a colon at the end, causing a syntax error.
🚀 Application
expert2:30remaining
Result of Modifying Circular Linked List During Traversal
Consider this code that removes the node with data 20 during traversal of a circular linked list. What is the printed output after modification?
DSA Python
class Node: def __init__(self, data): self.data = data self.next = None head = Node(10) second = Node(20) third = Node(30) head.next = second second.next = third third.next = head current = head prev = None output = [] while True: if current.data == 20: prev.next = current.next current = current.next continue output.append(str(current.data)) prev = current current = current.next if current == head: break print(' -> '.join(output) + ' -> null')
Attempts:
2 left
💡 Hint
Trace the values of prev and current step by step, especially during deletion and the continue statement.
✗ Incorrect
When current reaches 20, prev is 10, so prev.next = 30 removes 20 safely. The continue skips appending 20. Then appends 10 (earlier) and 30, stopping before head again. Output: 10 -> 30 -> null.