Challenge - 5 Problems
Circular Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this code detecting circular linked list?
Given the following Java code that checks if a linked list is circular, what will be printed?
DSA Java
class Node { int data; Node next; Node(int data) { this.data = data; this.next = null; } } public class Test { public static boolean isCircular(Node head) { if (head == null) return false; Node temp = head.next; while (temp != null && temp != head) { temp = temp.next; } return temp == head; } public static void main(String[] args) { Node head = new Node(1); head.next = new Node(2); head.next.next = new Node(3); head.next.next.next = head; // circular link System.out.println(isCircular(head)); } }
Attempts:
2 left
💡 Hint
Think about how the loop checks if the next node points back to the head.
✗ Incorrect
The linked list is made circular by pointing the last node's next to head. The method traverses nodes until it finds head again, so it returns true.
❓ Predict Output
intermediateWhat does this code print when the list is not circular?
Analyze the output of the following Java code that checks if a linked list is circular when the list is linear (not circular).
DSA Java
class Node { int data; Node next; Node(int data) { this.data = data; this.next = null; } } public class Test { public static boolean isCircular(Node head) { if (head == null) return false; Node temp = head.next; while (temp != null && temp != head) { temp = temp.next; } return temp == head; } public static void main(String[] args) { Node head = new Node(1); head.next = new Node(2); head.next.next = new Node(3); // no circular link System.out.println(isCircular(head)); } }
Attempts:
2 left
💡 Hint
The list ends with a null next pointer, so the loop stops without finding head again.
✗ Incorrect
Since the list is linear and does not loop back to head, the method returns false.
🔧 Debug
advancedWhat error occurs in this circular detection code?
Consider this Java method to detect if a linked list is circular. What error will it cause when run on a non-circular list?
DSA Java
public static boolean isCircular(Node head) {
Node slow = head;
Node fast = head.next;
while (fast != null && fast.next != null) {
if (slow == fast) return true;
slow = slow.next;
fast = fast.next.next;
}
return false;
}Attempts:
2 left
💡 Hint
This is Floyd's cycle detection algorithm. Check how fast and slow pointers move.
✗ Incorrect
The code safely checks fast and fast.next for null before moving pointers, so no NullPointerException occurs. It returns false if no cycle.
❓ Predict Output
advancedWhat is the output of this code with a single node circular list?
What will this Java code print when the linked list has only one node that points to itself?
DSA Java
class Node { int data; Node next; Node(int data) { this.data = data; this.next = null; } } public class Test { public static boolean isCircular(Node head) { if (head == null) return false; Node temp = head.next; while (temp != null && temp != head) { temp = temp.next; } return temp == head; } public static void main(String[] args) { Node head = new Node(1); head.next = head; // circular link to itself System.out.println(isCircular(head)); } }
Attempts:
2 left
💡 Hint
A single node pointing to itself forms a circular list.
✗ Incorrect
The method detects the next node is head itself, so it returns true.
🧠 Conceptual
expertWhy is Floyd's cycle detection algorithm preferred for circular linked list detection?
Which of the following is the main advantage of Floyd's cycle detection algorithm over the simple traversal method for detecting circular linked lists?
Attempts:
2 left
💡 Hint
Think about memory usage and pointer movement in Floyd's algorithm.
✗ Incorrect
Floyd's algorithm uses two pointers moving at different speeds to detect cycles without extra memory or modifying the list.
