Challenge - 5 Problems
Circular Singly Linked List Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Circular Singly Linked List after Insertions
What is the printed output of the circular singly linked list after inserting nodes with values 10, 20, and 30 in that order?
DSA Java
class Node { int data; Node next; Node(int data) { this.data = data; this.next = null; } } class CircularSinglyLinkedList { Node head = null; 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; } } 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 class Main { public static void main(String[] args) { CircularSinglyLinkedList 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 printList stops after one full cycle.
✗ Incorrect
The insert method adds nodes at the end, and the printList method prints nodes starting from head until it reaches head again, printing each node once. So the output is the sequence of inserted nodes followed by null.
🧠 Conceptual
intermediateUnderstanding Circular Singly Linked List Structure
Which statement correctly describes the structure of a circular singly linked list?
Attempts:
2 left
💡 Hint
Think about what makes the list circular.
✗ Incorrect
In a circular singly linked list, the last node's next pointer points back to the head node, creating a loop with no null at the end.
🔧 Debug
advancedIdentify the Bug in Circular List Insertion
What error will occur when running this insertion code for a circular singly linked list?
DSA Java
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 what happens to newNode.next when inserting the first node.
✗ Incorrect
When inserting the first node, newNode.next is set to null instead of head. When inserting the second node, the while loop condition (temp.next != head) evaluates to true (null != head), sets temp to null, and the next condition evaluation causes NullPointerException.
❓ Predict Output
advancedOutput After Deleting a Node in Circular Singly Linked List
What is the output after deleting the node with value 20 from the circular singly linked list containing 10 -> 20 -> 30 -> null?
DSA Java
class CircularSinglyLinkedList { Node head = null; 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; } } void delete(int data) { if (head == null) return; Node curr = head, prev = null; do { if (curr.data == data) { if (prev != null) { prev.next = curr.next; } else { // Deleting head node 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); } 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 class Main { public static void main(String[] args) { CircularSinglyLinkedList list = new CircularSinglyLinkedList(); list.insert(10); list.insert(20); list.insert(30); list.delete(20); list.printList(); } }
Attempts:
2 left
💡 Hint
Deleting node 20 should remove it and maintain circularity.
✗ Incorrect
After deleting node 20, the list contains nodes 10 and 30 linked circularly. The printList method prints 10 -> 30 -> null.
🧠 Conceptual
expertTime Complexity of Searching in Circular Singly Linked List
What is the time complexity of searching for a value in a circular singly linked list with n nodes?
Attempts:
2 left
💡 Hint
Consider how many nodes you might check in the worst case.
✗ Incorrect
Searching requires checking each node until the value is found or the list loops back to the head, so it takes linear time O(n). Circularity does not reduce search time.
