🔄 Detect Loop in Linked List

Use Floyd’s Cycle Detection (Tortoise & Hare) algorithm!

🎯 Problem: Detect Loop in Singly Linked List

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.

🐢🐇 Floyd’s Cycle Detection Algorithm

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.

  1. Initialize slow and fast at head
  2. Move slow by 1, fast by 2
  3. If slow == fast → loop detected (true)
  4. If fast reaches end (null) → no loop (false)

🎮 Interactive Demo

Enter values and choose whether to create a loop...

Empty List

📋 Detection Result