Challenge - 5 Problems
Circular Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of inserting at beginning in circular linked list
What is the printed state of the circular linked list after inserting 10 at the beginning?
DSA Python
class Node: def __init__(self, data): self.data = data self.next = None class CircularLinkedList: def __init__(self): self.head = None def insert_at_beginning(self, data): new_node = Node(data) if not self.head: new_node.next = new_node self.head = new_node else: temp = self.head while temp.next != self.head: temp = temp.next temp.next = new_node new_node.next = self.head self.head = new_node 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) + " -> null") cll = CircularLinkedList() cll.insert_at_beginning(20) cll.insert_at_beginning(30) cll.insert_at_beginning(10) cll.print_list()
Attempts:
2 left
💡 Hint
Remember, inserting at beginning means new node becomes the head and points to the old head.
✗ Incorrect
Inserting 10 at beginning makes it the new head. The list was 30 -> 20 -> null before insertion. After insertion, it becomes 10 -> 30 -> 20 -> null.
❓ Predict Output
intermediateOutput after inserting into empty circular linked list
What is the printed state of the circular linked list after inserting 5 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_at_beginning(self, data): new_node = Node(data) if not self.head: new_node.next = new_node self.head = new_node else: temp = self.head while temp.next != self.head: temp = temp.next temp.next = new_node new_node.next = self.head self.head = new_node 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) + " -> null") cll = CircularLinkedList() cll.insert_at_beginning(5) cll.print_list()
Attempts:
2 left
💡 Hint
When list is empty, new node points to itself.
✗ Incorrect
Inserting into empty circular linked list creates a single node pointing to itself. So printed list is '5 -> null'.
🔧 Debug
advancedIdentify the bug in insert_at_beginning method
What error will this code raise when inserting into 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_at_beginning(self, data): new_node = Node(data) if self.head is None: new_node.next = new_node self.head = new_node else: temp = self.head while temp.next != None: temp = temp.next temp.next = new_node new_node.next = self.head self.head = new_node cll = CircularLinkedList() cll.insert_at_beginning(1) cll.insert_at_beginning(2)
Attempts:
2 left
💡 Hint
Check the loop condition for finding last node in circular linked list.
✗ Incorrect
The loop condition 'while temp.next != None' never ends because in circular linked list last node points to head, not None. This causes infinite loop.
❓ Predict Output
advancedResulting list after multiple insertions at beginning
What is the printed state of the circular linked list after these insertions?
DSA Python
class Node: def __init__(self, data): self.data = data self.next = None class CircularLinkedList: def __init__(self): self.head = None def insert_at_beginning(self, data): new_node = Node(data) if not self.head: new_node.next = new_node self.head = new_node else: temp = self.head while temp.next != self.head: temp = temp.next temp.next = new_node new_node.next = self.head self.head = new_node 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) + " -> null") cll = CircularLinkedList() cll.insert_at_beginning(1) cll.insert_at_beginning(2) cll.insert_at_beginning(3) cll.insert_at_beginning(4) cll.print_list()
Attempts:
2 left
💡 Hint
Each new insertion at beginning becomes the new head.
✗ Incorrect
Nodes inserted at beginning in order 1,2,3,4 results in list 4 -> 3 -> 2 -> 1 -> null.
🧠 Conceptual
expertWhy update last node's next pointer when inserting at beginning?
In a circular linked list, why must we update the last node's next pointer when inserting a new node at the beginning?
Attempts:
2 left
💡 Hint
Think about what makes the list circular.
✗ Incorrect
In circular linked list, last node's next points to head. When head changes, last node's next must point to new head to keep circular structure.
