Detect if a Linked List is Circular in DSA Java - 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.
public boolean isCircular(Node head) {
if (head == null) return false;
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;
}
This code checks if a linked list has a cycle by moving two pointers at different speeds.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that moves two pointers through the list.
- How many times: At most, it runs once per node until pointers meet or list ends.
As the list gets longer, the number of steps grows roughly with the number of nodes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 steps |
| 100 | About 100 steps |
| 1000 | About 1000 steps |
Pattern observation: The steps increase linearly as the list size grows.
Time Complexity: O(n)
This means the time to detect a cycle grows in direct proportion to the list length.
[X] Wrong: "The fast pointer moves twice as fast, so the time is O(n/2), which is constant time."
[OK] Correct: Even though the fast pointer moves faster, the time still grows with the number of nodes, so it is linear, not constant.
Understanding this helps you explain how to efficiently find cycles, a common problem in coding interviews.
"What if we used only one pointer and checked visited nodes with extra memory? 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: Define circular linked list
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 BQuick 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: Identify Floyd's cycle detection logic
Two pointers move at different speeds: slow moves one step, fast moves two steps.Step 2: Check for meeting point
If slow and fast pointers meet, the list is circular; otherwise, if fast reaches null, it's not circular.Final Answer:
while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; if (slow == fast) return true; } -> Option AQuick Check:
Two pointers meet = circular [OK]
- Not checking fast.next for null
- Using only one pointer to detect loop
- Returning true when list is empty
class Node { int data; Node next; Node(int d) { data = d; next = null; } }
public class Test {
public static boolean isCircular(Node head) {
if (head == null) return false;
Node slow = head, fast = head.next;
while (fast != null && fast.next != null) {
if (slow == fast) return true;
slow = slow.next;
fast = fast.next.next;
}
return false;
}
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));
}
}Solution
Step 1: Understand the linked list structure
The list has nodes 1 -> 2 -> 3, and then 3 points back to 1, making it circular.Step 2: Trace the isCircular method
Slow starts at 1, fast at 2. They move until they meet because of the loop, so method returns true.Final Answer:
true -> Option DQuick Check:
Loop present = true [OK]
- Starting fast pointer incorrectly
- Not creating circular link properly
- Confusing output with false
public boolean isCircular(Node head) {
Node slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow.next == fast.next) return true;
}
return false;
}Solution
Step 1: Analyze pointer comparison
The code compares slow.next and fast.next, but to detect loop, slow and fast themselves must be compared.Step 2: Correct the comparison
Change if condition to if (slow == fast) to detect when pointers meet inside the loop.Final Answer:
The condition should compare slow and fast, not slow.next and fast.next -> Option CQuick Check:
Compare slow and fast pointers directly [OK]
- Comparing next nodes instead of current nodes
- Moving fast pointer incorrectly
- Wrong loop condition
Solution
Step 1: Detect loop using two pointers
Use Floyd's cycle detection to find when slow equals fast inside the loop.Step 2: Count loop length
Keep slow pointer fixed, move fast pointer one step at a time, incrementing count until fast meets slow again.Final Answer:
After detecting the loop with slow == fast, keep slow fixed and move fast until it meets slow again, counting steps -> Option AQuick Check:
Count steps in loop after detection [OK]
- Counting nodes before loop detection
- Using total nodes instead of loop length
- Not moving pointer correctly to count loop
Ready to prove it?
Pass the test and this pattern moves unknown
