Bird
0
0
DSA Cprogramming~20 mins

Detect if a Linked List is Circular in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Circular List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this code detecting a circular linked list?

Consider the following C code that checks if a linked list is circular. What will it print?

DSA C
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;
}
ACircular
BNot Circular
CSegmentation fault
DInfinite loop
Attempts:
2 left
💡 Hint

Check if the traversal reaches back to the head node.

Predict Output
intermediate
2:00remaining
What is the output when the linked list is not circular?

What will the following code print if the linked list is not circular?

DSA C
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;
}
AInfinite loop
BCircular
CSegmentation fault
DNot Circular
Attempts:
2 left
💡 Hint

Check if the last node points to NULL.

🔧 Debug
advanced
2:00remaining
Why does this code cause an infinite loop when checking circularity?

Examine the code below. Why does it cause an infinite loop when the list is circular?

DSA C
int isCircular(Node* head) {
    Node* temp = head;
    while (temp != NULL) {
        temp = temp->next;
    }
    return 0;
}
ABecause temp is never updated inside the loop
BBecause the function does not handle empty lists
CBecause the loop does not stop when temp equals head again
DBecause the loop condition should check temp->next, not temp
Attempts:
2 left
💡 Hint

Think about what happens when the list is circular and temp moves through nodes.

🧠 Conceptual
advanced
2:00remaining
Which method is best to detect a circular linked list efficiently?

Which of the following methods is the most efficient to detect if a linked list is circular?

ATraverse the list and check if any node's next pointer points to the head node
BUse two pointers moving at different speeds (slow and fast) to detect a cycle
CCount nodes and compare with a maximum limit to guess circularity
DStore visited nodes in an array and check for repeats
Attempts:
2 left
💡 Hint

Think about time and space efficiency.

Predict Output
expert
2:00remaining
What is the output of this Floyd's cycle detection code on a circular list?

Given the code below implementing Floyd's cycle detection, what will it print for a circular linked list?

DSA C
#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;
}
ACycle Detected
BSegmentation fault
CNo Cycle
DInfinite loop
Attempts:
2 left
💡 Hint

Floyd's algorithm detects cycles by meeting slow and fast pointers.