Bird
0
0
DSA Cprogramming~10 mins

Detect Cycle in Linked List Floyd's Algorithm in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Detect Cycle in Linked List Floyd's Algorithm
Start: slow = head, fast = head
Move slow by 1 step
Move fast by 2 steps
Check if fast or fast->next is NULL?
YesNo cycle, return false
No
Check if slow == fast?
YesCycle detected, return true
Back to Move slow by 1 step
The algorithm uses two pointers moving at different speeds to detect if a cycle exists by checking if they meet.
Execution Sample
DSA C
Node* slow = head;
Node* fast = head;
while (fast && fast->next) {
  slow = slow->next;
  fast = fast->next->next;
  if (slow == fast) return true;
}
return false;
This code moves two pointers through the list at different speeds to find if they meet, indicating a cycle.
Execution Table
StepOperationslow Pointerfast PointerCheck ConditionVisual State
1Initialize pointersNode1Node1fast and fast->next not NULL┌────────┐ ┌────────┐ ┌────────┐ │ data:1 │──→ │ data:2 │──→ │ data:3 │ │ next:──┼ │ next:──┼ │ next:──┼ └────────┘ └────────┘ └────────┘ head
2Move slow by 1Node2Node1fast and fast->next not NULLStep 2: slow moves to Node2 ┌────────┐ ┌────────┐ ┌────────┐ │ data:1 │ │ data:2 │──→ │ data:3 │ │ next:──┼──→ │ next:──┼ │ next:──┼ └────────┘ └────────┘ └────────┘ head
3Move fast by 2Node2Node3fast and fast->next not NULLStep 3: fast moves to Node3 ┌────────┐ ┌────────┐ ┌────────┐ │ data:1 │ │ data:2 │ │ data:3 │ │ next:──┼──→ │ next:──┼──→ │ next:──┼ └────────┘ └────────┘ └────────┘ head
4Check slow == fastNode2Node3No (Node2 != Node3)Pointers not equal, continue
5Move slow by 1Node3Node3fast and fast->next not NULLStep 5: slow moves to Node3 ┌────────┐ ┌────────┐ ┌────────┐ │ data:1 │ │ data:2 │ │ data:3 │ │ next:──┼──→ │ next:──┼──→ │ next:──┼ └────────┘ └────────┘ └────────┘ head
6Move fast by 2Node3Node2fast and fast->next not NULLStep 6: fast moves to Node2 (cycle) ┌────────┐ ┌────────┐ │ data:1 │ │ data:2 │ │ next:──┼──→ │ next:──┼──→ (cycle back to Node2) └────────┘ └────────┘ head
7Check slow == fastNode3Node2No (Node3 != Node2)Pointers not equal, continue
8Move slow by 1Node2Node2fast and fast->next not NULLStep 8: slow moves to Node2 ┌────────┐ ┌────────┐ │ data:1 │ │ data:2 │ │ next:──┼──→ │ next:──┼──→ (cycle) └────────┘ └────────┘ head
9Move fast by 2Node2Node2fast and fast->next not NULLStep 9: fast moves to Node2 Pointers meet, cycle detected
10Check slow == fastNode2Node2Yes (Node2 == Node2)Cycle detected, return true
💡 Pointers slow and fast meet at Node2, confirming a cycle
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 5After Step 6After Step 8After Step 9Final
slowNode1Node2Node2Node3Node3Node2Node2Node2
fastNode1Node1Node3Node3Node2Node2Node2Node2
Key Moments - 3 Insights
Why do we move fast pointer by two steps but slow pointer by one?
Moving fast pointer twice as fast ensures that if a cycle exists, fast will eventually catch slow inside the loop, as shown in steps 3, 6, and 9 in the execution_table.
What happens if fast or fast->next becomes NULL?
If fast or fast->next is NULL, it means the list ends and no cycle exists. This is checked before each move in the loop (see step 1 and exit condition).
Why do we check slow == fast after moving pointers?
Checking slow == fast after moving pointers detects if both pointers meet inside a cycle, confirming its presence (see steps 4, 7, and 10).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step do slow and fast pointers first meet?
AStep 9
BStep 7
CStep 4
DStep 10
💡 Hint
Check the 'Check slow == fast' column and 'Visual State' at steps 4, 7, 9, and 10.
According to variable_tracker, what is the position of fast pointer after Step 6?
ANode3
BNode2
CNode1
DNULL
💡 Hint
Look at the 'fast' row under 'After Step 6' in variable_tracker.
If the linked list had no cycle, what would happen to the fast pointer in the execution_table?
AIt would move only one step at a time
BIt would meet slow pointer
CIt would eventually become NULL or fast->next would be NULL
DIt would loop infinitely
💡 Hint
Refer to the exit condition in concept_flow and execution_table where fast or fast->next is NULL.
Concept Snapshot
Detect Cycle in Linked List using Floyd's Algorithm:
- Use two pointers: slow (1 step), fast (2 steps)
- Move pointers until fast or fast->next is NULL (no cycle)
- If slow == fast at any point, cycle exists
- Time: O(n), Space: O(1)
- Efficient and widely used cycle detection method
Full Transcript
This visualization shows Floyd's cycle detection algorithm in a linked list. Two pointers, slow and fast, start at the head. Slow moves one node at a time, fast moves two. If the list has no cycle, fast or fast->next becomes NULL, ending the loop. If there is a cycle, fast eventually catches slow inside the loop. The execution table traces each pointer move and checks if they meet. The variable tracker records pointer positions after each step. Key moments clarify why pointers move at different speeds and why meeting means a cycle. The quiz tests understanding of pointer positions and loop exit conditions.