Challenge - 5 Problems
Circular List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after inserting nodes in a circular singly linked list?
Consider the following C# code that inserts nodes into a circular singly linked list. What will be the printed output?
DSA C#
public class Node { public int data; public Node next; public Node(int data) { this.data = data; this.next = null; } } public class CircularSinglyLinkedList { public Node head = null; public void Insert(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; } } public void PrintList() { if (head == null) return; Node temp = head; do { System.Console.Write(temp.data + " -> "); temp = temp.next; } while (temp != head); System.Console.WriteLine("null"); } } var list = new CircularSinglyLinkedList(); list.Insert(10); list.Insert(20); list.Insert(30); list.PrintList();
Attempts:
2 left
💡 Hint
Remember that in a circular singly linked list, the last node points back to the head, but printing stops when we reach the head again.
✗ Incorrect
The Insert method adds nodes at the end and maintains the circular link by pointing the last node's next to head. The PrintList method prints nodes starting from head and stops when it loops back to head, so the output is 10 -> 20 -> 30 -> null.
❓ Predict Output
intermediateWhat is the output after deleting a node from a circular singly linked list?
Given the following code that deletes a node with a specific value from a circular singly linked list, what will be the printed output after deleting the node with value 20?
DSA C#
public class Node { public int data; public Node next; public Node(int data) { this.data = data; this.next = null; } } public class CircularSinglyLinkedList { public Node head = null; public void Insert(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; } } public void Delete(int key) { if (head == null) return; Node curr = head, prev = null; do { if (curr.data == key) { if (prev != null) { prev.next = curr.next; if (curr == head) head = curr.next; } else { Node temp = head; while (temp.next != head) { temp = temp.next; } if (head == head.next) { head = null; } else { temp.next = head.next; head = head.next; } } return; } prev = curr; curr = curr.next; } while (curr != head); } public void PrintList() { if (head == null) { System.Console.WriteLine("List is empty"); return; } Node temp = head; do { System.Console.Write(temp.data + " -> "); temp = temp.next; } while (temp != head); System.Console.WriteLine("null"); } } var list = new CircularSinglyLinkedList(); list.Insert(10); list.Insert(20); list.Insert(30); list.Delete(20); list.PrintList();
Attempts:
2 left
💡 Hint
Deleting a node removes it from the list and updates links to maintain circularity.
✗ Incorrect
The Delete method removes the node with data 20. The list then contains nodes 10 and 30 linked circularly. Printing stops after reaching head again, so output is 10 -> 30 -> null.
🔧 Debug
advancedIdentify the error in this circular singly linked list insertion code
What error will this code produce when inserting nodes into a circular singly linked list?
DSA C#
public class Node { public int data; public Node next; public Node(int data) { this.data = data; this.next = null; } } public class CircularSinglyLinkedList { public Node head = null; public void Insert(int data) { Node newNode = new Node(data); if (head == null) { head = newNode; newNode.next = null; } else { Node temp = head; while (temp.next != head) { temp = temp.next; } temp.next = newNode; newNode.next = head; } } }
Attempts:
2 left
💡 Hint
Check how the first node's next pointer is set.
✗ Incorrect
In the Insert method, when inserting the first node, newNode.next is set to null instead of pointing to head. This breaks the circular link, so the list is not circular.
❓ Predict Output
advancedWhat is the output after traversing a circular singly linked list with one node?
Given this code that creates a circular singly linked list with one node and prints it, what is the output?
DSA C#
public class Node { public int data; public Node next; public Node(int data) { this.data = data; this.next = null; } } public class CircularSinglyLinkedList { public Node head = null; public void Insert(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; } } public void PrintList() { if (head == null) { System.Console.WriteLine("List is empty"); return; } Node temp = head; do { System.Console.Write(temp.data + " -> "); temp = temp.next; } while (temp != head); System.Console.WriteLine("null"); } } var list = new CircularSinglyLinkedList(); list.Insert(100); list.PrintList();
Attempts:
2 left
💡 Hint
A single node points to itself in a circular singly linked list.
✗ Incorrect
The list has one node with data 100. The node's next points to itself. The PrintList method prints the node once and stops when it loops back to head, so output is 100 -> null.
🧠 Conceptual
expertWhat is the time complexity of inserting a node at the end of a circular singly linked list without a tail pointer?
Consider a circular singly linked list that only keeps a reference to the head node (no tail pointer). What is the time complexity of inserting a new node at the end of the list?
Attempts:
2 left
💡 Hint
Think about how you find the last node when you only have the head reference.
✗ Incorrect
Without a tail pointer, to insert at the end, you must traverse the entire list to find the last node, which takes O(n) time where n is the number of nodes.
