Bird
Raised Fist0
DSA Cprogramming~10 mins

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

Choose your learning style10 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?
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 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.

Practice

(1/5)
1. What is the main purpose of Floyd's Cycle Detection Algorithm in a linked list?
easy
A. To find the middle element of the linked list
B. To detect if there is a cycle (loop) in the linked list
C. To reverse the linked list
D. To sort the linked list in ascending order

Solution

  1. Step 1: Understand Floyd's Algorithm goal

    Floyd's Cycle Detection Algorithm is designed to find if a linked list has a cycle or loop.
  2. Step 2: Compare with other linked list operations

    Sorting, reversing, or finding the middle element are different operations and not related to cycle detection.
  3. Final Answer:

    To detect if there is a cycle (loop) in the linked list -> Option B
  4. Quick Check:

    Cycle detection = Detect cycle [OK]
Hint: Floyd's algorithm is famous for cycle detection [OK]
Common Mistakes:
  • Confusing cycle detection with sorting
  • Thinking it finds middle element
  • Assuming it reverses the list
2. Which of the following is the correct way to initialize the two pointers used in Floyd's Cycle Detection Algorithm in C?
easy
A. Node *slow = head; Node *fast = head->next;
B. Node *slow = NULL; Node *fast = head;
C. Node *slow = head->next; Node *fast = head->next->next;
D. Node *slow = head; Node *fast = head;

Solution

  1. Step 1: Understand pointer initialization in Floyd's algorithm

    Both slow and fast pointers start at the head node to begin traversal.
  2. Step 2: Check options for correct initialization

    Node *slow = head; Node *fast = head; initializes both pointers at head, which is the standard approach.
  3. Final Answer:

    Node *slow = head; Node *fast = head; -> Option D
  4. Quick Check:

    Both pointers start at head [OK]
Hint: Both pointers start at head node in Floyd's algorithm [OK]
Common Mistakes:
  • Starting fast pointer ahead of slow incorrectly
  • Initializing slow as NULL
  • Skipping initialization of one pointer
3. What will be the output of the following C code snippet using Floyd's algorithm on a linked list with a cycle?
typedef struct Node {
  int data;
  struct Node *next;
} Node;

int detectCycle(Node *head) {
  Node *slow = head, *fast = head;
  while (fast && fast->next) {
    slow = slow->next;
    fast = fast->next->next;
    if (slow == fast) return 1;
  }
  return 0;
}

// Assume linked list has a cycle
printf("%d", detectCycle(head));
medium
A. 1
B. 0
C. Compilation error
D. Infinite loop

Solution

  1. Step 1: Analyze the detectCycle function logic

    The function moves slow pointer by one and fast pointer by two nodes. If they meet, it returns 1 indicating a cycle.
  2. Step 2: Given the list has a cycle, pointers will meet

    Since the list has a cycle, slow and fast pointers will eventually meet, so function returns 1.
  3. Final Answer:

    1 -> Option A
  4. Quick Check:

    Cycle present = return 1 [OK]
Hint: Cycle means slow and fast pointers meet, return 1 [OK]
Common Mistakes:
  • Assuming function returns 0 for cycle
  • Thinking code causes infinite loop
  • Confusing return values
4. Identify the bug in this Floyd's cycle detection code snippet:
int detectCycle(Node *head) {
  Node *slow = head, *fast = head;
  while (fast != NULL && fast->next != NULL) {
    slow = slow->next;
    fast = fast->next;
    if (slow == fast) return 1;
    fast = fast->next;
  }
  return 0;
}
medium
A. fast pointer moves two steps but not checked for NULL before second move
B. slow pointer moves two steps instead of one
C. fast pointer moves only one step instead of two
D. slow pointer is not initialized properly

Solution

  1. Step 1: Analyze pointer movements in the loop

    Slow moves one step, fast moves one step, then checks if slow == fast, before the second fast move.
  2. Step 2: Problem with check timing

    In Floyd's algorithm, fast should move two steps before the cycle check. Here, the check occurs after fast has moved only one step, and the second fast move is done without checking if fast->next is NULL, which can cause a NULL pointer dereference.
  3. Final Answer:

    fast pointer moves two steps but not checked for NULL before second move -> Option A
  4. Quick Check:

    Second fast move lacks NULL check [OK]
Hint: Fast pointer must complete two steps with NULL checks before cycle check [OK]
Common Mistakes:
  • Checking cycle after fast's first step only
  • Confusing slow and fast pointer steps
  • Not initializing pointers correctly
5. You have a linked list where a cycle exists. After detecting the cycle using Floyd's algorithm, how can you find the starting node of the cycle?
hard
A. Move fast pointer two steps and slow pointer one step until they meet again
B. Count the number of nodes in the list and divide by two
C. Reset slow pointer to head, then move slow and fast one step at a time until they meet
D. Reverse the linked list and check for cycle again

Solution

  1. Step 1: Detect cycle and pointers meet inside the loop

    Floyd's algorithm detects cycle when slow and fast pointers meet inside the loop.
  2. Step 2: Reset slow to head and move both pointers one step at a time

    Reset slow to head, then move slow and fast one step each until they meet again; this meeting point is the cycle start.
  3. Final Answer:

    Reset slow pointer to head, then move slow and fast one step at a time until they meet -> Option C
  4. Quick Check:

    Reset slow + move both one step = cycle start [OK]
Hint: Reset slow to head, move both pointers stepwise to find cycle start [OK]
Common Mistakes:
  • Moving fast pointer two steps again
  • Trying to count nodes to find cycle start
  • Reversing list to detect cycle start
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard