Which of the following is the main advantage of using a circular linked list over a singly linked list?
Think about what happens when you reach the end of the list in both types.
A circular linked list connects the last node back to the first node, allowing continuous traversal without stopping. This is not possible in a singly linked list where the last node points to null.
What will be the output of the following code that traverses a circular linked list with 3 nodes?
class Node: def __init__(self, data): self.data = data self.next = None # Create nodes node1 = Node(10) node2 = Node(20) node3 = Node(30) # Link nodes in a circle node1.next = node2 node2.next = node3 node3.next = node1 # Traverse starting from node1, print 5 nodes current = node1 count = 0 while count < 5: print(current.data, end=' -> ') current = current.next count += 1 print('null')
Remember the list loops back to the first node after the last.
The circular linked list loops back to the first node after the last. Traversing 5 nodes starting at node1 prints 10, 20, 30, then again 10, 20.
Which real-world scenario is best modeled by a circular linked list?
Think about a system that cycles through items repeatedly without stopping.
A music playlist that repeats songs endlessly is a perfect example of a circular linked list, where after the last song, it loops back to the first song automatically.
What is the bug in the following code that inserts a new node at the end of a circular linked list?
def insert_end(head, data): new_node = Node(data) if not head: head = new_node new_node.next = new_node return head current = head while current.next != head: current = current.next current.next = new_node new_node.next = head return head
Check what happens when the list is empty and a new node is added.
When the list is empty, the new node's next should point to itself to maintain the circular property, but the code sets it to None, breaking the circle.
Why are circular linked lists preferred for implementing round-robin scheduling in operating systems?
Think about how the scheduler moves from one process to the next repeatedly.
Circular linked lists allow the scheduler to move from the last process back to the first seamlessly, enabling fair and continuous process execution without extra pointer resets.