Challenge - 5 Problems
Circular Linked List Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of inserting a node at the end of a circular linked list
What is the printed state of the circular linked list after inserting a node with value 4 at the end?
DSA C#
class Node { public int data; public Node next; public Node(int d) { data = d; next = null; } } class CircularLinkedList { public Node last; public CircularLinkedList() { last = null; } public void InsertEnd(int data) { Node newNode = new Node(data); if (last == null) { last = newNode; last.next = last; } else { newNode.next = last.next; last.next = newNode; last = newNode; } } public string PrintList() { if (last == null) return "null"; Node temp = last.next; string result = ""; do { result += temp.data + " -> "; temp = temp.next; } while (temp != last.next); result += "null"; return result; } } var cll = new CircularLinkedList(); cll.InsertEnd(1); cll.InsertEnd(2); cll.InsertEnd(3); cll.InsertEnd(4); Console.WriteLine(cll.PrintList());
Attempts:
2 left
💡 Hint
Remember that in a circular linked list, the last node points back to the first node.
✗ Incorrect
The InsertEnd method adds the new node after the last node and updates last to the new node. The list becomes 1 -> 2 -> 3 -> 4 -> null when printed.
❓ Predict Output
intermediateOutput after inserting into an empty circular linked list
What is the printed state of the circular linked list after inserting a node with value 10 into an empty list?
DSA C#
class Node { public int data; public Node next; public Node(int d) { data = d; next = null; } } class CircularLinkedList { public Node last; public CircularLinkedList() { last = null; } public void InsertEnd(int data) { Node newNode = new Node(data); if (last == null) { last = newNode; last.next = last; } else { newNode.next = last.next; last.next = newNode; last = newNode; } } public string PrintList() { if (last == null) return "null"; Node temp = last.next; string result = ""; do { result += temp.data + " -> "; temp = temp.next; } while (temp != last.next); result += "null"; return result; } } var cll = new CircularLinkedList(); cll.InsertEnd(10); Console.WriteLine(cll.PrintList());
Attempts:
2 left
💡 Hint
Inserting into an empty circular linked list creates a single node pointing to itself.
✗ Incorrect
When the list is empty, the new node points to itself, so printing shows 10 -> null.
🔧 Debug
advancedIdentify the error in InsertEnd method
What error will occur when running this InsertEnd method on a circular linked list?
DSA C#
public void InsertEnd(int data) { Node newNode = new Node(data); if (last == null) { last = newNode; last.next = null; } else { newNode.next = last.next; last.next = newNode; last = newNode; } }
Attempts:
2 left
💡 Hint
Check what happens when last.next is set to null in a circular linked list.
✗ Incorrect
Setting last.next to null breaks the circular link, so printing the list causes a NullReferenceException when traversing.
❓ Predict Output
advancedOutput after multiple InsertEnd calls
What is the printed state of the circular linked list after these insertions: InsertEnd(5), InsertEnd(7), InsertEnd(9)?
DSA C#
class Node { public int data; public Node next; public Node(int d) { data = d; next = null; } } class CircularLinkedList { public Node last; public CircularLinkedList() { last = null; } public void InsertEnd(int data) { Node newNode = new Node(data); if (last == null) { last = newNode; last.next = last; } else { newNode.next = last.next; last.next = newNode; last = newNode; } } public string PrintList() { if (last == null) return "null"; Node temp = last.next; string result = ""; do { result += temp.data + " -> "; temp = temp.next; } while (temp != last.next); result += "null"; return result; } } var cll = new CircularLinkedList(); cll.InsertEnd(5); cll.InsertEnd(7); cll.InsertEnd(9); Console.WriteLine(cll.PrintList());
Attempts:
2 left
💡 Hint
The last node always points to the first node in a circular linked list.
✗ Incorrect
Each InsertEnd adds a node after last and updates last. The list prints in insertion order: 5 -> 7 -> 9 -> null.
🧠 Conceptual
expertWhy update 'last' pointer when inserting at end in circular linked list?
Why is it necessary to update the 'last' pointer to the new node when inserting at the end of a circular linked list?
Attempts:
2 left
💡 Hint
Think about how the last pointer helps in adding nodes quickly at the end.
✗ Incorrect
Updating 'last' to the new node keeps track of the end of the list, enabling O(1) insertion at the end.
