Challenge - 5 Problems
Circular Linked List Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after deleting a node with value 3
What is the printed state of the circular linked list after deleting the node with value 3?
DSA C#
public class Node { public int data; public Node next; public Node(int d) { data = d; next = null; } } public class CircularLinkedList { public Node head = null; public void Append(int data) { Node newNode = new Node(data); if (head == null) { head = newNode; newNode.next = head; return; } Node temp = head; while (temp.next != head) { temp = temp.next; } temp.next = newNode; newNode.next = head; } public void DeleteNode(int key) { if (head == null) return; Node curr = head, prev = null; // If head is to be deleted if (head.data == key) { if (head.next == head) { head = null; return; } Node last = head; while (last.next != head) { last = last.next; } head = head.next; last.next = head; return; } prev = head; curr = head.next; while (curr != head && curr.data != key) { prev = curr; curr = curr.next; } if (curr != head && curr.data == key) { prev.next = curr.next; } } public string PrintList() { if (head == null) return "null"; Node temp = head; string result = ""; do { result += temp.data + " -> "; temp = temp.next; } while (temp != head); result += "null"; return result; } } var cll = new CircularLinkedList(); cll.Append(1); cll.Append(2); cll.Append(3); cll.Append(4); cll.DeleteNode(3); Console.WriteLine(cll.PrintList());
Attempts:
2 left
💡 Hint
Focus on how the DeleteNode method removes the node with the given key and updates the links.
✗ Incorrect
The node with value 3 is removed. The list links 2 directly to 4, maintaining circularity. The printed output shows the list starting from head (1) and ending with null.
❓ Predict Output
intermediateOutput after deleting the head node
What is the printed state of the circular linked list after deleting the node with value 1 (the head)?
DSA C#
var cll = new CircularLinkedList(); cll.Append(1); cll.Append(2); cll.Append(3); cll.Append(4); cll.DeleteNode(1); Console.WriteLine(cll.PrintList());
Attempts:
2 left
💡 Hint
Deleting the head node requires updating the head pointer and the last node's next pointer.
✗ Incorrect
Deleting the head (1) moves the head to the next node (2) and updates the last node's next to the new head, preserving circularity.
🔧 Debug
advancedIdentify the error in DeleteNode method when deleting a non-existent node
What happens when DeleteNode is called with a key not present in the list? Identify the error or behavior.
DSA C#
cll.DeleteNode(5);Attempts:
2 left
💡 Hint
Check the while loop condition and what happens after it ends when key is not found.
✗ Incorrect
When key is not found, curr becomes head again. The code then tries to access curr.data without checking if key was found, causing NullReferenceException.
🚀 Application
advancedHow to delete the only node in a circular linked list?
Given a circular linked list with a single node, what is the correct way to delete that node?
Attempts:
2 left
💡 Hint
Deleting the only node means the list becomes empty.
✗ Incorrect
When the list has only one node, deleting it means the list is empty, so head should be set to null.
🧠 Conceptual
expertWhy is it necessary to update the last node's next pointer when deleting the head?
In a circular linked list, why must the last node's next pointer be updated when the head node is deleted?
Attempts:
2 left
💡 Hint
Think about what keeps the circular linked list connected.
✗ Incorrect
The last node points to the head to keep the list circular. When head changes, last node's next must point to the new head to maintain the circle.
