A circular linked list connects the last node back to the first node, allowing continuous traversal without a null end. This is not possible in a singly linked list where the last node points to null.
Round-robin scheduling cycles through processes in a circular manner, making circular linked lists ideal for this use case.
class Node { int data; Node next; Node(int data) { this.data = data; } } public class CircularList { public static void main(String[] args) { 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 < 6) { // traverse twice System.out.print(current.data + " "); current = current.next; count++; } } }
The code traverses the circular linked list twice (6 nodes total). Since the list has 3 nodes linked circularly, the output repeats the sequence 1 2 3 twice.
class Node { int data; Node next; Node(int data) { this.data = data; } } public class CircularList { Node head = null; void insertEnd(int data) { Node newNode = new Node(data); if (head == null) { head = newNode; newNode.next = head; } else { Node temp = head; while (temp.next != head) { temp = temp.next; } temp.next = newNode; newNode.next = head; } } }
In a circular linked list, the last node's next points to head, never null. The while loop condition temp.next != null never becomes false, causing an infinite loop.
Circular linked lists connect the last player back to the first, making it easy to cycle through turns continuously without special conditions.
