Detect if a Linked List is Circular in DSA C - Time & Space Complexity
We want to know how long it takes to check if a linked list loops back on itself.
How does the time needed grow as the list gets longer?
Analyze the time complexity of the following code snippet.
// Detect if linked list is circular using two pointers
int isCircular(struct Node* head) {
struct Node *slow = head, *fast = head;
while (fast != NULL && fast->next != NULL) {
slow = slow->next;
fast = fast->next->next;
if (slow == fast) {
return 1; // Circular
}
}
return 0; // Not circular
}
This code uses two pointers moving at different speeds to find a loop in the list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that moves pointers through the list.
- How many times: Up to n times, where n is the number of nodes in the list.
As the list grows, the number of steps to detect a loop grows roughly in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 steps |
| 100 | About 100 steps |
| 1000 | About 1000 steps |
Pattern observation: The time grows directly with the size of the list.
Time Complexity: O(n)
This means the time to check grows linearly with the number of nodes in the list.
[X] Wrong: "The fast pointer moves twice as fast, so the time is O(n/2), which is much faster than O(n)."
[OK] Correct: We ignore constants in Big-O, so O(n/2) is still O(n). The time still grows linearly with list size.
Understanding this helps you explain how to detect loops efficiently, a common real-world problem in linked list handling.
"What if we used only one pointer moving one step at a time? How would the time complexity change?"
Practice
Solution
Step 1: Understand linked list structure
A linked list is a chain of nodes where each node points to the next node.Step 2: Identify circular linked list property
If the last node points back to any previous node, it creates a loop, making the list circular.Final Answer:
The last node points back to one of the previous nodes, forming a loop -> Option AQuick Check:
Circular linked list = last node points back [OK]
- Thinking circular means sorted
- Confusing empty list with circular
- Assuming single node list is always circular
Solution
Step 1: Analyze code logic for circular detection
The code moves through nodes until it finds the start node again, indicating a loop.Step 2: Check other options for correctness
if (head == NULL) return 1; else return 0; only checks if list is empty, not circular. while (head->next != NULL) { head = head->next; } return 1; stops at NULL, no loop check. for (int i = 0; i < 10; i++) { head = head->next; } return 0; blindly moves 10 steps without loop check.Final Answer:
while (head != NULL) { head = head->next; if (head == start) return 1; } return 0; -> Option DQuick Check:
Loop check by revisiting start node = while (head != NULL) { head = head->next; if (head == start) return 1; } return 0; [OK]
- Only checking for NULL without loop detection
- Using fixed iteration count instead of loop condition
- Returning true for empty list
typedef struct Node {
int data;
struct Node* next;
} Node;
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 a, b, c;
a.data = 1; b.data = 2; c.data = 3;
a.next = &b; b.next = &c; c.next = &a;
printf("%d", isCircular(&a));
return 0;
}Solution
Step 1: Understand linked list setup
Nodes a, b, c are linked in a circle: a->b->c->a.Step 2: Trace isCircular function
Function moves temp pointer until it finds head again or NULL. Here, temp will reach head again, so returns 1.Final Answer:
1 -> Option CQuick Check:
Loop detected, returns 1 [OK]
- Assuming NULL termination means circular
- Confusing return values 0 and 1
- Not initializing temp correctly
int isCircular(Node* head) {
Node* slow = head;
Node* fast = head->next;
while (fast != NULL && fast->next != NULL) {
if (slow == fast) return 1;
slow = slow->next;
fast = fast->next->next;
}
return 0;
}Solution
Step 1: Understand Floyd's cycle detection setup
Both slow and fast pointers should start at head to detect cycle properly.Step 2: Identify initialization error
fast is initialized to head->next, which can miss cycle detection if list has only one node or cycle starts at head.Final Answer:
fast pointer initialization should be head, not head->next -> Option BQuick Check:
Initialize fast = head for correct cycle detection [OK]
- Starting fast pointer one step ahead
- Swapping slow and fast pointer steps
- Ignoring null checks in loop condition
Solution
Step 1: Make the list circular by linking last node to head
Traverse the list to find the last node, then set its next pointer to head to form a loop.Step 2: Apply Floyd's cycle detection algorithm
Use slow and fast pointers starting at head to detect the cycle created.Final Answer:
1. Traverse to last node 2. Set last node's next to head 3. Use Floyd's algorithm to detect cycle -> Option AQuick Check:
Make circular then detect cycle with Floyd's algorithm [OK]
- Setting wrong node's next pointer
- Not traversing to last node correctly
- Using Floyd's algorithm before making list circular
Ready to prove it?
Pass the test and this pattern moves unknown
