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 Java
class Node { int data; Node next; Node(int d) { data = d; next = null; } } class CircularLinkedList { Node head; 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; } 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.data == key) { prev.next = curr.next; } } void printList() { if (head == null) { System.out.println("List is empty"); return; } Node temp = head; do { System.out.print(temp.data + " -> "); temp = temp.next; } while (temp != head); System.out.println("null"); } public static void main(String[] args) { CircularLinkedList cll = new CircularLinkedList(); cll.append(1); cll.append(2); cll.append(3); cll.append(4); cll.deleteNode(3); cll.printList(); } }
Attempts:
2 left
💡 Hint
Remember that deleting a node removes it from the circular chain, and the list remains circular.
✗ Incorrect
The node with value 3 is removed. The list after deletion is 1 -> 2 -> 4 -> null, maintaining circular links.
❓ Predict Output
intermediateOutput after deleting the head node
What is the printed state of the circular linked list after deleting the head node (value 1)?
DSA Java
class Node { int data; Node next; Node(int d) { data = d; next = null; } } class CircularLinkedList { Node head; 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; } void deleteNode(int key) { if (head == null) return; Node curr = head, prev = null; 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.data == key) { prev.next = curr.next; } } void printList() { if (head == null) { System.out.println("List is empty"); return; } Node temp = head; do { System.out.print(temp.data + " -> "); temp = temp.next; } while (temp != head); System.out.println("null"); } public static void main(String[] args) { CircularLinkedList cll = new CircularLinkedList(); cll.append(1); cll.append(2); cll.append(3); cll.append(4); cll.deleteNode(1); cll.printList(); } }
Attempts:
2 left
💡 Hint
Deleting the head node means the head pointer moves to the next node.
✗ Incorrect
After deleting the head node (1), the new head is 2, and the list is 2 -> 3 -> 4 -> null.
🔧 Debug
advancedIdentify the error in this deleteNode method
What error will occur when running this deleteNode method on a circular linked list?
DSA Java
void deleteNode(int key) { if (head == null) return; Node curr = head, prev = null; if (head.data == key) { if (head.next == head) { head = null; return; } Node last = head; while (last.next != null) { // <-- Problem here 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.data == key) { prev.next = curr.next; } }
Attempts:
2 left
💡 Hint
Think about the condition in the while loop for finding the last node in a circular linked list.
✗ Incorrect
In a circular linked list, last.next never becomes null, so the loop runs infinitely.
❓ Predict Output
advancedOutput after deleting the only node in the list
What is the output after deleting the only node (value 5) in the circular linked list?
DSA Java
class Node { int data; Node next; Node(int d) { data = d; next = null; } } class CircularLinkedList { Node head; 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; } void deleteNode(int key) { if (head == null) return; 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; } Node prev = head; Node curr = head.next; while (curr != head && curr.data != key) { prev = curr; curr = curr.next; } if (curr.data == key) { prev.next = curr.next; } } void printList() { if (head == null) { System.out.println("List is empty"); return; } Node temp = head; do { System.out.print(temp.data + " -> "); temp = temp.next; } while (temp != head); System.out.println("null"); } public static void main(String[] args) { CircularLinkedList cll = new CircularLinkedList(); cll.append(5); cll.deleteNode(5); cll.printList(); } }
Attempts:
2 left
💡 Hint
Deleting the only node should empty the list.
✗ Incorrect
After deleting the only node, head becomes null, so the list is empty.
🧠 Conceptual
expertWhy is it important to update the last node's next pointer when deleting the head?
In a circular linked list, when deleting the head node, why must we update the last node's next pointer to the new head?
Attempts:
2 left
💡 Hint
Think about what keeps the circular linked list circular.
✗ Incorrect
The last node's next pointer must point to the new head to keep the circular structure intact.
