Challenge - 5 Problems
Circular Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Circular Linked List Detection Function
What is the output of the following C++ code that checks if a linked list is circular?
DSA C++
struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
bool isCircular(Node* head) {
if (!head) return false;
Node* temp = head->next;
while (temp != nullptr && temp != head) {
temp = temp->next;
}
return (temp == head);
}
int main() {
Node* head = new Node(1);
head->next = new Node(2);
head->next->next = new Node(3);
head->next->next->next = head; // Making list circular
bool result = isCircular(head);
if (result) {
printf("Circular\n");
} else {
printf("Not Circular\n");
}
return 0;
}Attempts:
2 left
💡 Hint
Think about how the function moves through the list and what condition it checks to decide circularity.
✗ Incorrect
The function starts from the node after head and moves forward. If it reaches back to head, the list is circular. Since the last node points back to head, the function returns true and prints 'Circular'.
🧠 Conceptual
intermediateUnderstanding Circular Linked List Detection Logic
Which statement best describes how the function detects if a linked list is circular?
Attempts:
2 left
💡 Hint
Focus on the stopping conditions of the traversal in the function.
✗ Incorrect
The function moves through the list starting from the second node. It stops if it reaches null (not circular) or if it reaches the head again (circular).
🔧 Debug
advancedIdentify the Error in Circular Linked List Detection Code
What error will occur when running this code snippet to detect circular linked list?
DSA C++
struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
bool isCircular(Node* head) {
if (!head) return false;
Node* temp = head->next;
while (temp != nullptr && temp != head) {
temp = temp->next;
}
return (temp == head);
}
int main() {
Node* head = new Node(1);
head->next = new Node(2);
head->next->next = new Node(3);
head->next->next->next = head->next; // Cycle created: 3 -> 2 (not back to head)
bool result = isCircular(head);
if (result) {
printf("Circular\n");
} else {
printf("Not Circular\n");
}
return 0;
}Attempts:
2 left
💡 Hint
Check the initial value of temp and the loop condition carefully.
✗ Incorrect
temp starts at head->next (node 2). The traversal enters the cycle 2 -> 3 -> 2, never reaching nullptr or head, causing an infinite loop.
❓ Predict Output
advancedOutput of Circular Linked List Detection with Single Node
What is the output of this code that checks if a single-node linked list is circular?
DSA C++
struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
bool isCircular(Node* head) {
if (!head) return false;
Node* temp = head->next;
while (temp != nullptr && temp != head) {
temp = temp->next;
}
return (temp == head);
}
int main() {
Node* head = new Node(10);
head->next = head; // Single node pointing to itself
bool result = isCircular(head);
if (result) {
printf("Circular\n");
} else {
printf("Not Circular\n");
}
return 0;
}Attempts:
2 left
💡 Hint
Consider what happens when the single node points to itself.
✗ Incorrect
Since the single node points to itself, the traversal reaches back to head immediately, so the function returns true and prints 'Circular'.
🧠 Conceptual
expertWhy is Floyd’s Cycle Detection Algorithm Preferred Over Simple Traversal?
Why is Floyd’s Cycle Detection (tortoise and hare) algorithm preferred for detecting circular linked lists instead of simple traversal checking if next is head?
Attempts:
2 left
💡 Hint
Think about detecting cycles that do not necessarily loop back to the head node.
✗ Incorrect
Floyd’s algorithm detects any cycle in the list by using two pointers moving at different speeds. It can detect loops anywhere, not just those that point back to the head.
