Linked List Cycle Visualizer & Step-by-Step Algorithm Solution

Detect whether a linked list contains a cycle using Floyd's Tortoise and Hare algorithm.

Category: linked-list | Difficulty: Easy

Tags: Linked List, Two Pointers, Cycle Detection, Floyd's Algorithm

Linked List Cycle

3
2
0
-4
100%
state
slownull
fastnull
Initialization
1/16
Explanation

List initialized with a cycle: tail node -4 connects back to node 2.

Source Code
1function hasCycle(head) {
2 let slow = head;
3 let fast = head;
4 while (fast !== null && fast.next !== null) {
5 slow = slow.next;
6 fast = fast.next.next;
7 if (slow === fast) return true;
8 }
9 return false;
10}