← Back to DSA Animator
Linked List Cycle LC #141 Easy Floyd's Cycle Detection
Problem

Given the head of a linked list, determine if the linked list has a cycle in it. A cycle exists if there is some node in the list that can be reached again by continuously following the next pointer. Return true if there is a cycle, false otherwise.

Example 1
Input: head = [3,2,0,-4], pos = 1
Output: true
Explanation: Tail connects back to node at index 1 (value 2), forming a cycle.
Example 2
Input: head = [1,2], pos = 0
Output: true
Explanation: Tail connects back to node at index 0 (value 1).
Example 3
Input: head = [1], pos = -1
Output: false
Explanation: No cycle — fast pointer will reach null.
Constraints: 0 ≤ n ≤ 10⁴  |  -10⁵ ≤ Node.val ≤ 10⁵  |  pos is -1 or a valid index
Try Examples
Approach
Floyd's Tortoise & Hare — Two-Pointer Cycle Detection
Use a slow pointer (moves 1 step) and a fast pointer (moves 2 steps). If a cycle exists, fast laps slow and they meet inside the loop. If no cycle, fast reaches null.
Linked List
CYCLE DETECTED — slow == fast !
NO CYCLE — fast reached null
State Variables
slow index
fast index
steps taken
0
Step Logic
Press ▶ Play or Next Step to begin the animation.
🎉
Ready
0 / 0
Select an example above and press Play.
Algorithm
1
Init slow = head, fast = head
2
Loop while fast != null && fast.next != null
3
Move slow = slow.next (+1 step)
4
Move fast = fast.next.next (+2 steps)
5
If slow == fast → cycle detected, return true
6
fast reached null → no cycle, return false
Time
O(n)
Space
O(1)
Why It Works

If a cycle exists, fast enters the loop first. From that point, every iteration reduces the gap between slow and fast by 1 (fast gains 2, slow gains 1 → net gain 1 per iteration). The gap starts at most L (loop length), so they meet within L additional iterations — O(n) total.

If no cycle, fast will reach a null node (or a node whose .next is null) within O(n/2) steps.