Use Floyd’s Cycle Detection (Tortoise & Hare) algorithm!
Hi! I'm Teju 👋 We need to determine if a singly linked list has a loop (i.e., is circular or has a cycle).
In this demo, you can create both circular (has loop) and non-circular (no loop) lists.
Note: Use the toggle to choose whether the list should be circular (has loop) or linear (no loop).
We will use Floyd's Tortoise and Hare algorithm to detect the loop efficiently in O(n) time and O(1) space.
Two pointers: Slow (tortoise) moves 1 step, Fast (hare) moves 2 steps.
If they ever meet → there is a loop!
If fast reaches null → no loop.
Enter values and choose whether to create a loop...
Loop Detected?