Which statement correctly describes the key difference between a circular linked list and a linear linked list?
Think about how the last node connects in each list type.
The main difference is that in a circular linked list, the last node links back to the first node, forming a circle. In a linear linked list, the last node points to null, marking the end.
Consider the following C# code that creates a circular linked list with three nodes and traverses it once. What is the printed output?
class Node { public int data; public Node next; public Node(int d) { data = d; next = null; } } class Program { static void Main() { Node head = new Node(1); Node second = new Node(2); Node third = new Node(3); head.next = second; second.next = third; third.next = head; // circular link Node current = head; int count = 0; while (count < 3) { System.Console.Write(current.data + " -> "); current = current.next; count++; } System.Console.WriteLine("null"); } }
The loop runs exactly 3 times, printing each node once.
The code traverses the circular linked list exactly 3 times, printing nodes 1, 2, and 3, then prints 'null' to mark the end of output.
What error will occur when running this C# code that tries to traverse a linear linked list?
class Node { public int data; public Node next; public Node(int d) { data = d; next = null; } } class Program { static void Main() { Node head = new Node(1); Node second = new Node(2); head.next = second; Node current = head; while (current.next != null) { System.Console.Write(current.data + " -> "); current = current.next; } System.Console.Write(current.data + " -> null"); } }
Check the loop condition and how current moves.
The loop runs while current.next is not null, printing nodes 1 and 2 correctly. The last node's next is null, so loop ends safely. No error occurs.
What is the output after adding a new node with value 4 at the end of this circular linked list and traversing all 4 nodes once?
class Node { public int data; public Node next; public Node(int d) { data = d; next = null; } } class Program { static void Main() { Node head = new Node(1); Node second = new Node(2); Node third = new Node(3); head.next = second; second.next = third; third.next = head; // circular link // Add new node with value 4 at end Node fourth = new Node(4); third.next = fourth; fourth.next = head; Node current = head; int count = 0; while (count < 4) { System.Console.Write(current.data + " -> "); current = current.next; count++; } System.Console.WriteLine("null"); } }
After adding the new node, the list has 4 nodes linked circularly.
The new node 4 is linked after node 3 and points back to head. Traversing 4 nodes prints 1, 2, 3, 4 then 'null'.
Which reason best explains why circular linked lists are preferred over linear linked lists in certain applications?
Think about how traversal works in circular linked lists.
Circular linked lists allow traversal to continue from the last node back to the first node seamlessly, which is useful in applications like round-robin scheduling.
