Challenge - 5 Problems
Circular Linked List Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output after inserting a node at the end of a circular linked list
What is the printed state of the circular linked list after inserting a new node with value 4 at the end?
DSA Python
class Node: def __init__(self, data): self.data = data self.next = None class CircularLinkedList: def __init__(self): self.head = None def insert_end(self, data): new_node = Node(data) if not self.head: self.head = new_node new_node.next = new_node return temp = self.head while temp.next != self.head: temp = temp.next temp.next = new_node new_node.next = self.head def print_list(self): if not self.head: print('List is empty') return temp = self.head result = [] while True: result.append(str(temp.data)) temp = temp.next if temp == self.head: break print(' -> '.join(result) + ' -> (head)') cll = CircularLinkedList() cll.insert_end(1) cll.insert_end(2) cll.insert_end(3) cll.insert_end(4) cll.print_list()
Attempts:
2 left
💡 Hint
Remember that in a circular linked list, the last node points back to the head.
✗ Incorrect
After inserting 4 at the end, the list contains nodes 1, 2, 3, and 4 in order, and the last node points back to the head node 1. The print_list method shows this sequence ending with '-> (head)'.
❓ Predict Output
intermediate2:00remaining
Output after inserting into an empty circular linked list
What is the printed state of the circular linked list after inserting a single node with value 10 into an empty list?
DSA Python
class Node: def __init__(self, data): self.data = data self.next = None class CircularLinkedList: def __init__(self): self.head = None def insert_end(self, data): new_node = Node(data) if not self.head: self.head = new_node new_node.next = new_node return temp = self.head while temp.next != self.head: temp = temp.next temp.next = new_node new_node.next = self.head def print_list(self): if not self.head: print('List is empty') return temp = self.head result = [] while True: result.append(str(temp.data)) temp = temp.next if temp == self.head: break print(' -> '.join(result) + ' -> (head)') cll = CircularLinkedList() cll.insert_end(10) cll.print_list()
Attempts:
2 left
💡 Hint
When the list is empty, the new node points to itself.
✗ Incorrect
Inserting into an empty circular linked list creates a single node that points to itself. The print shows the single node value followed by '-> (head)'.
🔧 Debug
advanced2:00remaining
Identify the error in circular linked list insertion code
What error will this code produce when inserting a node at the end of a circular linked list?
DSA Python
class Node: def __init__(self, data): self.data = data self.next = None class CircularLinkedList: def __init__(self): self.head = None def insert_end(self, data): new_node = Node(data) if not self.head: self.head = new_node new_node.next = new_node return temp = self.head while temp.next != None: temp = temp.next temp.next = new_node new_node.next = self.head cll = CircularLinkedList() cll.insert_end(5) cll.insert_end(6) cll.insert_end(7)
Attempts:
2 left
💡 Hint
Check the condition in the while loop that finds the last node.
✗ Incorrect
The while loop condition 'temp.next != None' never becomes false in a circular linked list because the last node points back to head, not None. This causes an infinite loop.
❓ Predict Output
advanced2:00remaining
Output after multiple insertions at the end of circular linked list
What is the printed state of the circular linked list after inserting nodes with values 10, 20, 30, 40 in that order at the end?
DSA Python
class Node: def __init__(self, data): self.data = data self.next = None class CircularLinkedList: def __init__(self): self.head = None def insert_end(self, data): new_node = Node(data) if not self.head: self.head = new_node new_node.next = new_node return temp = self.head while temp.next != self.head: temp = temp.next temp.next = new_node new_node.next = self.head def print_list(self): if not self.head: print('List is empty') return temp = self.head result = [] while True: result.append(str(temp.data)) temp = temp.next if temp == self.head: break print(' -> '.join(result) + ' -> (head)') cll = CircularLinkedList() for val in [10, 20, 30, 40]: cll.insert_end(val) cll.print_list()
Attempts:
2 left
💡 Hint
Nodes are inserted at the end, so order is preserved.
✗ Incorrect
Nodes are inserted at the end, so the list contains 10, 20, 30, 40 in order, and the last node points back to the head.
🧠 Conceptual
expert2:00remaining
Why is the last node's next pointer important in a circular linked list?
In a circular linked list, what is the main reason the last node's next pointer points back to the head node?
Attempts:
2 left
💡 Hint
Think about how you can keep moving through nodes without stopping.
✗ Incorrect
The last node points back to the head to create a loop, allowing traversal to cycle through the list repeatedly without a null end.