Detect Cycle in Linked List Floyd's Algorithm in DSA Javascript - Time & Space Complexity
We want to understand how long it takes to check if a linked list has a cycle using Floyd's Algorithm.
How does the time needed grow as the list gets longer?
Analyze the time complexity of the following code snippet.
function hasCycle(head) {
let slow = head;
let fast = head;
while (fast !== null && fast.next !== null) {
slow = slow.next;
fast = fast.next.next;
if (slow === fast) {
return true;
}
}
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 moves two pointers through the list.
- How many times: The slow pointer moves one step each loop, fast pointer moves two steps.
As the list length (n) grows, the slow pointer moves through nodes one by one, and the fast pointer moves twice as fast.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 5 steps to detect or finish |
| 100 | About 50 steps |
| 1000 | About 500 steps |
Pattern observation: The number of steps grows roughly in direct proportion to the list size.
Time Complexity: O(n)
This means the time to detect a cycle grows linearly with the number of nodes in the list.
[X] Wrong: "Because there are two pointers, the time complexity is doubled or squared."
[OK] Correct: Both pointers move through the list together, so the total steps still grow linearly, not exponentially.
Understanding this linear time check shows you can analyze pointer movement and loops clearly, a key skill in many coding problems.
"What if we used only one pointer moving one step at a time and stored visited nodes? How would the time complexity change?"
Practice
Solution
Step 1: Understand Floyd's Algorithm goal
Floyd's Cycle Detection Algorithm is designed to detect if a linked list has a cycle, meaning a node points back to a previous node.Step 2: Compare with other linked list operations
Sorting, reversing, or finding the middle element are different operations and not the purpose of this algorithm.Final Answer:
To check if the linked list contains a cycle or loop -> Option DQuick Check:
Detect cycle = Check loop presence [OK]
- Confusing cycle detection with sorting
- Thinking it finds the middle node
- Assuming it reverses the list
Solution
Step 1: Understand pointer initialization
Slow pointer starts at head, fast pointer starts at head.next to allow fast to move two steps safely.Step 2: Check pointer positions
Starting fast at head.next prevents immediate equality and helps detect cycles correctly.Final Answer:
let slow = head; let fast = head.next; -> Option CQuick Check:
Slow at head, fast at head.next [OK]
- Starting both pointers at head causing immediate equality
- Initializing slow pointer as null
- Starting pointers at incorrect nodes
function hasCycle(head) {
let slow = head;
let fast = head;
while (fast && fast.next) {
slow = slow.next;
fast = fast.next.next;
if (slow === fast) {
return true;
}
}
return false;
}
// Linked list: 1 -> 2 -> 3 -> 4 -> 2 (cycle back to node with value 2)What will be the output of
hasCycle(head)?Solution
Step 1: Understand the linked list structure
The list has a cycle because node with value 4 points back to node with value 2.Step 2: Trace Floyd's Algorithm behavior
Slow moves one step, fast moves two steps; they will meet inside the cycle, so function returns true.Final Answer:
true -> Option BQuick Check:
Cycle present = true [OK]
- Assuming no cycle returns true
- Confusing pointer equality with value equality
- Ignoring fast pointer null checks
function detectCycle(head) {
let slow = head.next;
let fast = head.next.next;
while (fast && fast.next) {
if (slow === fast) return true;
slow = slow.next;
fast = fast.next.next;
}
return false;
}What is the main issue with this code?
Solution
Step 1: Analyze pointer initialization
Code sets slow = head.next and fast = head.next.next without checking if head or head.next exist.Step 2: Identify potential runtime error
If head is null or head.next is null, accessing next properties causes runtime error.Final Answer:
It does not check if head is null before accessing next -> Option AQuick Check:
Missing null check causes runtime error [OK]
- Ignoring null checks causing runtime errors
- Starting pointers incorrectly but missing null checks
- Confusing node equality with value equality
Solution
Step 1: Detect cycle using Floyd's Algorithm
When slow and fast pointers meet, a cycle is confirmed.Step 2: Find cycle start node
Reset slow pointer to head, then move slow and fast one step at a time; their meeting point is the cycle start.Final Answer:
Reset one pointer to head, then move both pointers one step at a time until they meet -> Option AQuick Check:
Reset and move pointers to find cycle start [OK]
- Continuing fast pointer movement after cycle detection
- Trying to reverse list to find cycle start
- Using node count to find cycle start incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
