Consider the following C code that checks if a linked list is circular. What will it print?
typedef struct Node {
int data;
struct Node* next;
} Node;
#include <stdio.h>
#include <stdlib.h>
int isCircular(Node* head) {
if (head == NULL) return 0;
Node* temp = head->next;
while (temp != NULL && temp != head) {
temp = temp->next;
}
return (temp == head);
}
int main() {
Node* head = (Node*)malloc(sizeof(Node));
Node* second = (Node*)malloc(sizeof(Node));
Node* third = (Node*)malloc(sizeof(Node));
head->data = 1; head->next = second;
second->data = 2; second->next = third;
third->data = 3; third->next = head; // circular link
if (isCircular(head)) {
printf("Circular\n");
} else {
printf("Not Circular\n");
}
return 0;
}Check if the traversal reaches back to the head node.
The list is circular because the last node points back to the head. The function returns true and prints "Circular".
What will the following code print if the linked list is not circular?
typedef struct Node {
int data;
struct Node* next;
} Node;
#include <stdio.h>
#include <stdlib.h>
int isCircular(Node* head) {
if (head == NULL) return 0;
Node* temp = head->next;
while (temp != NULL && temp != head) {
temp = temp->next;
}
return (temp == head);
}
int main() {
Node* head = (Node*)malloc(sizeof(Node));
Node* second = (Node*)malloc(sizeof(Node));
Node* third = (Node*)malloc(sizeof(Node));
head->data = 1; head->next = second;
second->data = 2; second->next = third;
third->data = 3; third->next = NULL; // not circular
if (isCircular(head)) {
printf("Circular\n");
} else {
printf("Not Circular\n");
}
return 0;
}Check if the last node points to NULL.
The last node points to NULL, so the list is not circular. The function returns false and prints "Not Circular".
Examine the code below. Why does it cause an infinite loop when the list is circular?
int isCircular(Node* head) { Node* temp = head; while (temp != NULL) { temp = temp->next; } return 0; }
Think about what happens when the list is circular and temp moves through nodes.
The loop only stops when temp is NULL, but in a circular list temp never becomes NULL. The condition to stop when temp equals head again is missing, causing an infinite loop.
Which of the following methods is the most efficient to detect if a linked list is circular?
Think about time and space efficiency.
The two-pointer method (Floyd's cycle detection) detects cycles in O(n) time and O(1) space, making it efficient.
Given the code below implementing Floyd's cycle detection, what will it print for a circular linked list?
#include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node* next; } Node; int detectCycle(Node* head) { Node *slow = head, *fast = head; while (fast != NULL && fast->next != NULL) { slow = slow->next; fast = fast->next->next; if (slow == fast) { return 1; } } return 0; } int main() { Node* head = (Node*)malloc(sizeof(Node)); Node* second = (Node*)malloc(sizeof(Node)); Node* third = (Node*)malloc(sizeof(Node)); head->data = 1; head->next = second; second->data = 2; second->next = third; third->data = 3; third->next = head; // circular if (detectCycle(head)) { printf("Cycle Detected\n"); } else { printf("No Cycle\n"); } return 0; }
Floyd's algorithm detects cycles by meeting slow and fast pointers.
The slow and fast pointers meet inside the cycle, so the function returns 1 and prints "Cycle Detected".
