Floyd's cycle-finding algorithm(Tortoise & Hare)

Floyd's cycle-finding algorithm(Tortoise & Hare)

Floyd's cycle-finding algorithm :-

  • The idea is to have two references to the list and move them at different speeds. Move one forward by 1 node and the other by 2 nodes. If the linked list has a loop they will definitely meet.
  • Else either of the two references(or their next) will become null.


below are the total ways we can use to detect the cycle ..

Method 1( Hash Table ) :- Traverse the Linked List and store the node pointers in a hash table. As soon as you discover a collision, you’ve discovered a loop!

Method 2( List Reversal):- Traverse the Linked List and start reversing it. If there is a loop, you shall revisit the head again. However, the “loop” in the list has been reversed — to fix that, traverse the list again, reversing the pointers!

Method 3( Mark Visited Node ):- Traverse the Linked List and keep marking the node as visited. if you re-visit same node means there is a loop in the given Linked List.

Method 4(Floyd’s Cycle-Finding Algorithm or Two Pointers Algorithm) :- Traverse the Linked List using two pointers. slower pointer moves one step while faster pointer moves forward two steps. If these pointers meet at some node, then there is a loop.


To view or add a comment, sign in

More articles by 🧿 🟨Saral Saxena 🧑‍💻🏆

  • Run .http Files in a Intellij idea

    When developing APIs in a Spring Boot project, testing endpoints is a crucial part of the workflow. Tools like Postman…

  • Mastering API Versioning in Spring Boot: Stop Breaking Your Endpoints

    Introduction API versioning is one of the most overlooked yet critical aspects of API development. Many developers…

  • Blazing Fast Performance in Spring Boot 3 with CDS

    Spring Boot 3 brings several performance optimizations, but one of the most exciting ones is Class Data Sharing (CDS)…

  • Java 21 Docker Optimization

    Java 21 introduces significant improvements and features, particularly in containerization. This article explores five…

  • Java Optimization: 7000ms → 90ms

    When working with two separate lists and trying to match data based on a common id, many developers often default to…

  • Spring Boot Red Flags

    Spring Boot is a powerful framework designed to simplify Java application development by offering production-ready…

  • Stop Writing != null

    Null pointer exceptions are a familiar pain point in Java development. The go-to solution for many developers? Add a…

  • Spring boot Apps getting optimized

    Before making any changes, established clear performance baselines. Here’s what our initial metrics looked like:…

  • Validating Payloads with Spring Boot 3.4.0

    First, let’s examine a controller that receives a object. This object contains fields such as: first name, last name…

  • Limitations of Java Executor Framework.

    The Java Executor Framework has inherent limitations that affect its performance in high-throughput, low-latency…

Insights from the community

Others also viewed

Explore topics