Challenge - 5 Problems
Circular Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after inserting 10 at the beginning?
Given the circular linked list: 20 -> 30 -> 40 -> (back to 20), what is the list after inserting 10 at the beginning?
DSA Java
class Node { int data; Node next; Node(int data) { this.data = data; this.next = null; } } class CircularLinkedList { Node head; void insertAtBeginning(int data) { Node newNode = new Node(data); if (head == null) { newNode.next = newNode; head = newNode; } else { Node temp = head; while (temp.next != head) { temp = temp.next; } newNode.next = head; temp.next = newNode; head = newNode; } } void printList() { if (head == null) return; Node temp = head; do { System.out.print(temp.data + " -> "); temp = temp.next; } while (temp != head); System.out.print("null"); } } public class Main { public static void main(String[] args) { CircularLinkedList cll = new CircularLinkedList(); cll.head = new Node(20); Node second = new Node(30); Node third = new Node(40); cll.head.next = second; second.next = third; third.next = cll.head; cll.insertAtBeginning(10); cll.printList(); } }
Attempts:
2 left
💡 Hint
Remember to update the last node's next pointer to the new head.
✗ Incorrect
Inserting at the beginning means the new node becomes the head. The last node's next pointer must point to this new head to maintain circularity. So the list becomes 10 -> 20 -> 30 -> 40 -> back to 10, printed as 10 -> 20 -> 30 -> 40 -> null.
❓ Predict Output
intermediateWhat is the output after inserting 50 at the beginning of an empty list?
Given an empty circular linked list, what is the output after inserting 50 at the beginning?
DSA Java
class Node { int data; Node next; Node(int data) { this.data = data; this.next = null; } } class CircularLinkedList { Node head; void insertAtBeginning(int data) { Node newNode = new Node(data); if (head == null) { newNode.next = newNode; head = newNode; } else { Node temp = head; while (temp.next != head) { temp = temp.next; } newNode.next = head; temp.next = newNode; head = newNode; } } void printList() { if (head == null) return; Node temp = head; do { System.out.print(temp.data + " -> "); temp = temp.next; } while (temp != head); System.out.print("null"); } } public class Main { public static void main(String[] args) { CircularLinkedList cll = new CircularLinkedList(); cll.insertAtBeginning(50); cll.printList(); } }
Attempts:
2 left
💡 Hint
When the list is empty, the new node points to itself.
✗ Incorrect
When inserting into an empty circular linked list, the new node points to itself and becomes the head. So the list contains one node with data 50, printed as 50 -> null.
🔧 Debug
advancedWhy does this insertion code cause an infinite loop?
Consider this code snippet for inserting at the beginning of a circular linked list. Why does it cause an infinite loop when printing?
DSA Java
void insertAtBeginning(int data) { Node newNode = new Node(data); if (head == null) { head = newNode; } else { Node temp = head; while (temp.next != head) { temp = temp.next; } newNode.next = head; temp.next = newNode; head = newNode; } } void printList() { Node temp = head; while (temp != null) { System.out.print(temp.data + " -> "); temp = temp.next; } System.out.print("null"); }
Attempts:
2 left
💡 Hint
Think about how to detect the end of a circular linked list.
✗ Incorrect
In a circular linked list, there is no null next pointer. Using while(temp != null) causes an infinite loop because temp never becomes null. The correct approach is to loop until temp reaches head again.
❓ Predict Output
advancedWhat is the output after multiple insertions at beginning?
Starting with an empty circular linked list, insert 5, then 15, then 25 at the beginning. What is the final list?
DSA Java
class Node { int data; Node next; Node(int data) { this.data = data; this.next = null; } } class CircularLinkedList { Node head; void insertAtBeginning(int data) { Node newNode = new Node(data); if (head == null) { newNode.next = newNode; head = newNode; } else { Node temp = head; while (temp.next != head) { temp = temp.next; } newNode.next = head; temp.next = newNode; head = newNode; } } void printList() { if (head == null) return; Node temp = head; do { System.out.print(temp.data + " -> "); temp = temp.next; } while (temp != head); System.out.print("null"); } } public class Main { public static void main(String[] args) { CircularLinkedList cll = new CircularLinkedList(); cll.insertAtBeginning(5); cll.insertAtBeginning(15); cll.insertAtBeginning(25); cll.printList(); } }
Attempts:
2 left
💡 Hint
Each new node becomes the new head.
✗ Incorrect
Inserting at beginning means the newest inserted node is the head. So after inserting 5, then 15, then 25, the list is 25 -> 15 -> 5 -> null.
🧠 Conceptual
expertWhy is updating the last node's next pointer necessary when inserting at beginning?
In a circular linked list, when inserting a new node at the beginning, why must we update the last node's next pointer to the new node?
Attempts:
2 left
💡 Hint
Think about what makes the list circular.
✗ Incorrect
The circular linked list loops back from the last node to the head. When head changes, the last node must point to the new head to keep the circle intact.
