
- Data Structures & Algorithms
- DSA - Home
- DSA - Overview
- DSA - Environment Setup
- Algorithm
- DSA - Algorithms Basics
- DSA - Asymptotic Analysis
- DSA - Greedy Algorithms
- DSA - Divide and Conquer
- DSA - Dynamic Programming
- Data Structures
- DSA - Data Structure Basics
- DSA - Data Structures and Types
- DSA - Array Data Structure
- Linked Lists
- DSA - Linked List Basics
- DSA - Doubly Linked List
- DSA - Circular Linked List
- Stack & Queue
- DSA - Stack
- DSA - Expression Parsing
- DSA - Queue
- Searching Techniques
- DSA - Linear Search
- DSA - Binary Search
- DSA - Interpolation Search
- DSA - Hash Table
- Sorting Techniques
- DSA - Sorting Algorithms
- DSA - Bubble Sort
- DSA - Insertion Sort
- DSA - Selection Sort
- DSA - Merge Sort
- DSA - Shell Sort
- DSA - Quick Sort
- Graph Data Structure
- DSA - Graph Data Structure
- DSA - Depth First Traversal
- DSA - Breadth First Traversal
- Tree Data Structure
- DSA - Tree Data Structure
- DSA - Tree Traversal
- DSA - Binary Search Tree
- DSA - AVL Tree
- DSA - Red Black Trees
- DSA - B Trees
- DSA - B+ Trees
- DSA - Splay Trees
- DSA - Spanning Tree
- DSA - Tries
- DSA - Heap
- Recursion
- DSA - Recursion Basics
- DSA - Tower of Hanoi
- DSA - Fibonacci Series
- DSA Useful Resources
- DSA - Questions and Answers
- DSA - Quick Guide
- DSA - Useful Resources
- DSA - Discussion
Floyd Cycle Detection Algorithm to detect the cycle in a linear Data Structure
Floyd Cycle is one of the cycle detection algorithms to detect the cycle in a given singly linked list.
In the Floyd Cycle algorithm, we have two pointers that initially point at the head. In Hare and Tortoise’s story, Hare moves twice as fast as Tortoise, and whenever the hare reaches the end of the path, the tortoise reaches the middle of the path.
Algorithm
Initialize Hare and Tortoise at the head node of the List.
Initially, the hare moves twice as fast as the tortoise.
Move the hare and tortoise both and find if the hare reaches the end of the Linked List, return as there is no loop in the list.
Otherwise, both Hare and Tortoise will go forward.
If Hare and Tortoise are at the same Node, then return since we have found the list cycle.
Else, start with step 2.
Pseudocode for the above algorithm
tortoise := headNode hare := headNode foreach: if hare == end return 'There is No Loop Found.' hare := hare.next if hare == end return 'No Loop Found' hare = hare.next tortoise = tortoise.next if hare == tortoise return 'Cycle Detected'
- Related Articles
- Detect Cycle in a Directed Graph
- Detect Cycle in a an Undirected Graph
- Python Program to Detect the Cycle in a Linked List
- Python Program for Detect Cycle in a Directed Graph
- Lysogenic Cycle-Definition, Structure, and Steps
- Floyd Warshall Algorithm
- The Floyd-Warshall algorithm in Javascript
- Life Cycle Phases of Data Analytics
- Linear Probing in Data Structure
- Algorithm Specification-Introduction in Data Structure
- Gross Operating Cycle Vs Net Operating Cycle
- Project Life Cycle Vs. Product Life Cycle
- Algorithm to construct an Expression Tree in Data Structure
- Explain the Difference Between Linear and Non-linear Data Structure
- Cycle Sort
