0
0
DSA Pythonprogramming~20 mins

Traversal of Circular Linked List in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Circular Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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')
A10 -> 20 -> null
B10 -> 20 -> 30 -> 10 -> null
CRuntimeError: infinite loop
D10 -> 20 -> 30 -> null
Attempts:
2 left
💡 Hint
Remember that traversal stops when we reach the head node again.
🧠 Conceptual
intermediate
1: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?
AThe number of nodes in the list
BOne less than the number of nodes
CInfinite nodes because of the loop
DZero nodes because head is repeated
Attempts:
2 left
💡 Hint
Traversal stops when you reach the head again, so count all unique nodes once.
🔧 Debug
advanced
2: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')
ANo error, prints all nodes correctly
BSkips the last node in the list
CInfinite loop occurs
DAttributeError because current is None
Attempts:
2 left
💡 Hint
Check if the last node's data is printed or skipped.
📝 Syntax
advanced
1: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
ANo syntax error
BMissing colon after while True
CIncorrect comparison operator in if statement
DMissing parentheses in print statement
Attempts:
2 left
💡 Hint
Check the syntax of the while loop line.
🚀 Application
expert
2: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')
ARuntimeError due to NoneType attribute access
B10 -> 20 -> 30 -> null
C10 -> 30 -> null
DInfinite loop without stopping
Attempts:
2 left
💡 Hint
Trace the values of prev and current step by step, especially during deletion and the continue statement.