Challenge - 5 Problems
Circular Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after inserting at beginning in circular linked list
What is the printed state of the circular linked list after inserting 10 at the beginning when the list initially contains 20 -> 30 -> 40 -> back to 20?
DSA C#
class Node { public int data; public Node next; public Node(int d) { data = d; next = null; } } class CircularLinkedList { public Node last; public void InsertAtBeginning(int data) { Node newNode = new Node(data); if (last == null) { last = newNode; last.next = last; } else { newNode.next = last.next; last.next = newNode; } } public string PrintList() { if (last == null) return "empty"; Node temp = last.next; string result = ""; do { result += temp.data + " -> "; temp = temp.next; } while (temp != last.next); result += "back to " + last.next.data; return result; } } var cll = new CircularLinkedList(); cll.last = new Node(40); cll.last.next = new Node(20); cll.last.next.next = new Node(30); cll.last.next.next.next = cll.last; cll.InsertAtBeginning(10); System.Console.WriteLine(cll.PrintList());
Attempts:
2 left
💡 Hint
Remember that in a circular linked list, the last node points to the first node. Inserting at the beginning means changing the last node's next pointer.
✗ Incorrect
The InsertAtBeginning method adds a new node after the last node and points it to the previous first node. The last node remains the same, so the list starts at the new node. Hence the order is 10 -> 20 -> 30 -> 40 -> back to 10.
🧠 Conceptual
intermediateUnderstanding pointer changes when inserting at beginning
When inserting a new node at the beginning of a circular linked list, which pointer must be updated to maintain the circular structure?
Attempts:
2 left
💡 Hint
Think about how the new node connects to the existing list and how the last node points to the first node.
✗ Incorrect
The new node's next pointer must point to the old first node, and the last node's next pointer must point to the new node to keep the circular link intact.
🔧 Debug
advancedIdentify the bug in InsertAtBeginning method
What error will occur if the InsertAtBeginning method does not set last.next = last when the list is initially empty?
DSA C#
public void InsertAtBeginning(int data) { Node newNode = new Node(data); if (last == null) { last = newNode; // Missing last.next = last; } else { newNode.next = last.next; last.next = newNode; } }
Attempts:
2 left
💡 Hint
Think about what happens when traversing a circular linked list if the last node's next pointer is null.
✗ Incorrect
If last.next is not set to last when the list is empty, the list is not circular. Traversing will reach a null pointer causing errors or infinite loops.
❓ Predict Output
advancedResult after multiple insertions at beginning
Given an empty circular linked list, what is the printed state after inserting 5, then 15, then 25 at the beginning in that order?
DSA C#
class Node { public int data; public Node next; public Node(int d) { data = d; next = null; } } class CircularLinkedList { public Node last; public void InsertAtBeginning(int data) { Node newNode = new Node(data); if (last == null) { last = newNode; last.next = last; } else { newNode.next = last.next; last.next = newNode; } } public string PrintList() { if (last == null) return "empty"; Node temp = last.next; string result = ""; do { result += temp.data + " -> "; temp = temp.next; } while (temp != last.next); result += "back to " + last.next.data; return result; } } var cll = new CircularLinkedList(); cll.InsertAtBeginning(5); cll.InsertAtBeginning(15); cll.InsertAtBeginning(25); System.Console.WriteLine(cll.PrintList());
Attempts:
2 left
💡 Hint
Each insertion at beginning adds a new node after last and before the old first node.
✗ Incorrect
After inserting 5, list is 5 -> back to 5. Then inserting 15 at beginning makes 15 -> 5 -> back to 15. Then inserting 25 at beginning makes 25 -> 15 -> 5 -> back to 25.
🧠 Conceptual
expertTime complexity of inserting at beginning in circular linked list
What is the time complexity of inserting a node at the beginning of a circular linked list when you have a pointer to the last node?
Attempts:
2 left
💡 Hint
Consider how many nodes you need to visit to insert at the beginning if you have the last node pointer.
✗ Incorrect
Since you have direct access to the last node, you can insert the new node after last in constant time without traversing the list.
