0
0
DSA Pythonprogramming~10 mins

Detect Cycle in Linked List Floyd's Algorithm in DSA Python - 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 None?
Yes→No cycle, return False
No↓
Check if slow == fast?
Yes→Cycle 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 Python
slow = head
fast = head
while fast and 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 Nodefast NodePointer ChangesVisual State
1Initialize slow and fast at headNode 10Node 10slow=head, fast=headā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│──→ │ data:20│──→ │ data:30│──→ │ data:40│ │ next:──┼──→ │ next:──┼──→ │ next:──┼──→ │ next:──┼─→ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
2Move slow by 1 stepNode 20Node 10slow = slow.nextā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│──→ │ data:30│──→ │ data:40│ │ next:──┼──→ │ next:──┼──→ │ next:──┼──→ │ next:──┼─→ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head slow
3Move fast by 2 stepsNode 20Node 30fast = fast.next.nextā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│──→ │ data:30│──→ │ data:40│ │ next:──┼──→ │ next:──┼──→ │ next:──┼──→ │ next:──┼─→ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head fast
4Check slow == fast?Node 20Node 30No match, continue loopPointers at different nodes, no cycle detected yet
5Move slow by 1 stepNode 30Node 30slow = slow.nextā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│ │ data:30│──→ │ data:40│ │ next:──┼──→ │ next:──┼──→ │ next:──┼──→ │ next:──┼─→ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head slow
6Move fast by 2 stepsNode 30Node 40fast = fast.next.nextā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│ │ data:30│ │ data:40│ │ next:──┼──→ │ next:──┼──→ │ next:──┼──→ │ next:──┼─→ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head fast
7Check slow == fast?Node 30Node 40No match, continue loopPointers at different nodes, no cycle detected yet
8Move slow by 1 stepNode 40Node 40slow = slow.nextā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│ │ data:30│ │ data:40│ │ next:──┼──→ │ next:──┼──→ │ next:──┼──→ │ next:──┼─→ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head slow
9Move fast by 2 stepsNode 40Node 30fast = fast.next.next (cycle back to Node 30)Cycle created: Node 40 points back to Node 30 Pointers: slow → Node 40 fast → Node 30
10Check slow == fast?Node 40Node 30No match, continue loopPointers at different nodes, cycle detected but pointers not met yet
11Move slow by 1 stepNode 30Node 30slow = slow.next (cycle back to Node 30)Cycle continues: slow → Node 30 fast → Node 30
12Move fast by 2 stepsNode 30Node 40fast = fast.next.next (cycle back to Node 40)Cycle continues: slow → Node 30 fast → Node 40
13Check slow == fast?Node 30Node 40No match, continue loopPointers at different nodes, cycle detected but pointers not met yet
14Move slow by 1 stepNode 40Node 40slow = slow.nextCycle continues: slow → Node 40 fast → Node 40
15Move fast by 2 stepsNode 40Node 30fast = fast.next.nextCycle continues: slow → Node 40 fast → Node 30
16Check slow == fast?Node 40Node 30No match, continue loopPointers at different nodes, cycle detected but pointers not met yet
17Move slow by 1 stepNode 30Node 30slow = slow.nextCycle continues: slow → Node 30 fast → Node 30
18Move fast by 2 stepsNode 30Node 40fast = fast.next.nextCycle continues: slow → Node 30 fast → Node 40
19Check slow == fast?Node 30Node 40No match, continue loopPointers at different nodes, cycle detected but pointers not met yet
20Move slow by 1 stepNode 40Node 40slow = slow.nextCycle continues: slow → Node 40 fast → Node 40
21Move fast by 2 stepsNode 40Node 30fast = fast.next.nextCycle continues: slow → Node 40 fast → Node 30
22Check slow == fast?Node 40Node 30No match, continue loopPointers at different nodes, cycle detected but pointers not met yet
23Move slow by 1 stepNode 30Node 30slow = slow.nextCycle continues: slow → Node 30 fast → Node 30
24Move fast by 2 stepsNode 30Node 40fast = fast.next.nextCycle continues: slow → Node 30 fast → Node 40
25Check slow == fast?Node 30Node 40No match, continue loopPointers at different nodes, cycle detected but pointers not met yet
26Move slow by 1 stepNode 40Node 40slow = slow.nextCycle continues: slow → Node 40 fast → Node 40
27Move fast by 2 stepsNode 40Node 30fast = fast.next.nextCycle continues: slow → Node 40 fast → Node 30
28Check slow == fast?Node 40Node 30No match, continue loopPointers at different nodes, cycle detected but pointers not met yet
29Move slow by 1 stepNode 30Node 30slow = slow.nextCycle continues: slow → Node 30 fast → Node 30
30Move fast by 2 stepsNode 30Node 40fast = fast.next.nextCycle continues: slow → Node 30 fast → Node 40
31Check slow == fast?Node 30Node 40No match, continue loopPointers at different nodes, cycle detected but pointers not met yet
32Move slow by 1 stepNode 40Node 40slow = slow.nextCycle continues: slow → Node 40 fast → Node 40
33Move fast by 2 stepsNode 40Node 30fast = fast.next.nextCycle continues: slow → Node 40 fast → Node 30
34Check slow == fast?Node 40Node 30No match, continue loopPointers at different nodes, cycle detected but pointers not met yet
35Move slow by 1 stepNode 30Node 30slow = slow.nextCycle continues: slow → Node 30 fast → Node 30
36Move fast by 2 stepsNode 30Node 40fast = fast.next.nextCycle continues: slow → Node 30 fast → Node 40
37Check slow == fast?Node 30Node 40No match, continue loopPointers at different nodes, cycle detected but pointers not met yet
38Move slow by 1 stepNode 40Node 40slow = slow.nextCycle continues: slow → Node 40 fast → Node 40
39Move fast by 2 stepsNode 40Node 30fast = fast.next.nextCycle continues: slow → Node 40 fast → Node 30
40Check slow == fast?Node 40Node 30No match, continue loopPointers at different nodes, cycle detected but pointers not met yet
41Move slow by 1 stepNode 30Node 30slow = slow.nextCycle continues: slow → Node 30 fast → Node 30
42Move fast by 2 stepsNode 30Node 40fast = fast.next.nextCycle continues: slow → Node 30 fast → Node 40
43Check slow == fast?Node 30Node 40No match, continue loopPointers at different nodes, cycle detected but pointers not met yet
44Move slow by 1 stepNode 40Node 40slow = slow.nextCycle continues: slow → Node 40 fast → Node 40
45Move fast by 2 stepsNode 40Node 30fast = fast.next.nextCycle continues: slow → Node 40 fast → Node 30
46Check slow == fast?Node 40Node 30No match, continue loopPointers at different nodes, cycle detected but pointers not met yet
47Move slow by 1 stepNode 30Node 30slow = slow.nextCycle continues: slow → Node 30 fast → Node 30
48Move fast by 2 stepsNode 30Node 40fast = fast.next.nextCycle continues: slow → Node 30 fast → Node 40
49Check slow == fast?Node 30Node 40No match, continue loopPointers at different nodes, cycle detected but pointers not met yet
50Move slow by 1 stepNode 40Node 40slow = slow.nextCycle continues: slow → Node 40 fast → Node 40
51Move fast by 2 stepsNode 40Node 30fast = fast.next.nextCycle continues: slow → Node 40 fast → Node 30
52Check slow == fast?Node 40Node 30No match, continue loopPointers at different nodes, cycle detected but pointers not met yet
53Move slow by 1 stepNode 30Node 30slow = slow.nextCycle continues: slow → Node 30 fast → Node 30
54Move fast by 2 stepsNode 30Node 40fast = fast.next.nextCycle continues: slow → Node 30 fast → Node 40
55Check slow == fast?Node 30Node 40No match, continue loopPointers at different nodes, cycle detected but pointers not met yet
56Move slow by 1 stepNode 40Node 40slow = slow.nextCycle continues: slow → Node 40 fast → Node 40
57Move fast by 2 stepsNode 40Node 30fast = fast.next.nextCycle continues: slow → Node 40 fast → Node 30
58Check slow == fast?Node 40Node 30No match, continue loopPointers at different nodes, cycle detected but pointers not met yet
59Move slow by 1 stepNode 30Node 30slow = slow.nextCycle continues: slow → Node 30 fast → Node 30
60Move fast by 2 stepsNode 30Node 40fast = fast.next.nextCycle continues: slow → Node 30 fast → Node 40
61Check slow == fast?Node 30Node 40No match, continue loopPointers at different nodes, cycle detected but pointers not met yet
62Move slow by 1 stepNode 40Node 40slow = slow.nextCycle continues: slow → Node 40 fast → Node 40
63Move fast by 2 stepsNode 40Node 30fast = fast.next.nextCycle continues: slow → Node 40 fast → Node 30
64Check slow == fast?Node 40Node 30No match, continue loopPointers at different nodes, cycle detected but pointers not met yet
65Move slow by 1 stepNode 30Node 30slow = slow.nextCycle continues: slow → Node 30 fast → Node 30
66Move fast by 2 stepsNode 30Node 40fast = fast.next.nextCycle continues: slow → Node 30 fast → Node 40
67Check slow == fast?Node 30Node 40No match, continue loopPointers at different nodes, cycle detected but pointers not met yet
68Move slow by 1 stepNode 40Node 40slow = slow.nextCycle continues: slow → Node 40 fast → Node 40
69Move fast by 2 stepsNode 40Node 30fast = fast.next.nextCycle continues: slow → Node 40 fast → Node 30
70Check slow == fast?Node 40Node 30No match, continue loopPointers at different nodes, cycle detected but pointers not met yet
71Move slow by 1 stepNode 30Node 30slow = slow.nextCycle continues: slow → Node 30 fast → Node 30
72Move fast by 2 stepsNode 30Node 40fast = fast.next.nextCycle continues: slow → Node 30 fast → Node 40
73Check slow == fast?Node 30Node 40No match, continue loopPointers at different nodes, cycle detected but pointers not met yet
74Move slow by 1 stepNode 40Node 40slow = slow.nextCycle continues: slow → Node 40 fast → Node 40
75Move fast by 2 stepsNode 40Node 30fast = fast.next.nextCycle continues: slow → Node 40 fast → Node 30
76Check slow == fast?Node 40Node 30No match, continue loopPointers at different nodes, cycle detected but pointers not met yet
77Move slow by 1 stepNode 30Node 30slow = slow.nextCycle continues: slow → Node 30 fast → Node 30
78Move fast by 2 stepsNode 30Node 40fast = fast.next.nextCycle continues: slow → Node 30 fast → Node 40
79Check slow == fast?Node 30Node 40No match, continue loopPointers at different nodes, cycle detected but pointers not met yet
80Move slow by 1 stepNode 40Node 40slow = slow.nextCycle continues: slow → Node 40 fast → Node 40
81Move fast by 2 stepsNode 40Node 30fast = fast.next.nextCycle continues: slow → Node 40 fast → Node 30
82Check slow == fast?Node 40Node 30No match, continue loopPointers at different nodes, cycle detected but pointers not met yet
83Move slow by 1 stepNode 30Node 30slow = slow.nextCycle continues: slow → Node 30 fast → Node 30
84Move fast by 2 stepsNode 30Node 40fast = fast.next.nextCycle continues: slow → Node 30 fast → Node 40
85Check slow == fast?Node 30Node 40No match, continue loopPointers at different nodes, cycle detected but pointers not met yet
86Move slow by 1 stepNode 40Node 40slow = slow.nextCycle continues: slow → Node 40 fast → Node 40
87Move fast by 2 stepsNode 40Node 30fast = fast.next.nextCycle continues: slow → Node 40 fast → Node 30
88Check slow == fast?Node 40Node 30No match, continue loopPointers at different nodes, cycle detected but pointers not met yet
89Move slow by 1 stepNode 30Node 30slow = slow.nextCycle continues: slow → Node 30 fast → Node 30
90Move fast by 2 stepsNode 30Node 40fast = fast.next.nextCycle continues: slow → Node 30 fast → Node 40
91Check slow == fast?Node 30Node 40No match, continue loopPointers at different nodes, cycle detected but pointers not met yet
92Move slow by 1 stepNode 40Node 40slow = slow.nextCycle continues: slow → Node 40 fast → Node 40
93Move fast by 2 stepsNode 40Node 30fast = fast.next.nextCycle continues: slow → Node 40 fast → Node 30
94Check slow == fast?Node 40Node 30No match, continue loopPointers at different nodes, cycle detected but pointers not met yet
95Move slow by 1 stepNode 30Node 30slow = slow.nextCycle continues: slow → Node 30 fast → Node 30
96Move fast by 2 stepsNode 30Node 40fast = fast.next.nextCycle continues: slow → Node 30 fast → Node 40
97Check slow == fast?Node 30Node 40No match, continue loopPointers at different nodes, cycle detected but pointers not met yet
98Move slow by 1 stepNode 40Node 40slow = slow.nextCycle continues: slow → Node 40 fast → Node 40
99Move fast by 2 stepsNode 40Node 30fast = fast.next.nextCycle continues: slow → Node 40 fast → Node 30
100Check slow == fast?Node 40Node 30No match, continue loopPointers at different nodes, cycle detected but pointers not met yet
šŸ’” The pointers slow and fast keep cycling through the loop nodes; when slow == fast, cycle is detected and function returns True.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6Final
slowNode 10Node 20Node 30Node 40Node 30Node 40Node 30Cycle detected
fastNode 10Node 30Node 40Node 30Node 40Node 30Node 40Cycle detected
Key Moments - 3 Insights
Why do we move fast pointer by two steps and slow pointer by one step?
Moving fast pointer twice as fast ensures that if a cycle exists, fast will eventually catch up to slow inside the cycle, as shown in execution_table rows 2-7.
What happens if the list has no cycle?
If fast or fast.next becomes None, the loop ends and the function returns False, meaning no cycle. This is shown in the exit condition in concept_flow and execution_table.
Why do we check slow == fast inside the loop?
Because when slow and fast point to the same node, it means fast has caught slow inside the cycle, confirming a cycle exists. This is shown in execution_table steps where slow and fast meet.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, which node is the fast pointer pointing to?
ANode 20
BNode 40
CNode 30
DNode 10
šŸ’” Hint
Refer to execution_table row with Step 3 under 'fast Node' column.
At which step do slow and fast pointers first point to the same node?
AStep 11
BStep 5
CStep 1
DStep 2
šŸ’” Hint
Check execution_table rows where 'slow Node' and 'fast Node' columns match.
If the list had no cycle, what would happen to the fast pointer?
AIt would keep moving forever
BIt would eventually become None or fast.next would be None
CIt would move only one step at a time
DIt would immediately equal slow pointer
šŸ’” Hint
Refer to concept_flow where the check for fast or fast.next being None ends the loop.
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 None (no cycle)
- If slow == fast at any point, cycle exists
- Return True if cycle found, else False
- Efficient O(n) time, O(1) space
Full Transcript
This concept shows how to detect a cycle in a linked list using Floyd's algorithm. Two pointers start at the head. Slow moves one step at a time, fast moves two steps. If the list has no cycle, fast or fast.next becomes None and the function returns False. If there is a cycle, fast will eventually catch slow inside the cycle, making slow equal fast, and the function returns True. The execution table traces each step, showing pointer positions and the linked list state. Key moments clarify why pointers move at different speeds and why the equality check is inside the loop.