Skip to content

Commit 1d6d698

Browse files
authored
Merge pull request #135 from BioHazard786/problem/linked-list-cycle
Implement Floyd's Cycle Detection Algorithm for linked list cycle detection
2 parents aaeac15 + a40efa0 commit 1d6d698

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

Q) Linked List/LinkedListCycle.cpp

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
/**
6+
* @brief Definition for singly-linked list node
7+
*
8+
* This structure represents a single node in a singly-linked list.
9+
* Each node contains an integer value and a pointer to the next node.
10+
*/
11+
struct ListNode
12+
{
13+
int val; ///< The integer value stored in the node
14+
ListNode *next; ///< Pointer to the next node in the list (NULL if last node)
15+
16+
/**
17+
* @brief Constructor to initialize a ListNode
18+
* @param x The integer value to store in the node
19+
*/
20+
ListNode(int x) : val(x), next(NULL) {}
21+
};
22+
23+
/**
24+
* @brief Solution class containing cycle detection algorithm
25+
*
26+
* This class implements Floyd's Cycle Detection Algorithm (also known as
27+
* "Tortoise and Hare" algorithm) to detect if a linked list contains a cycle.
28+
*/
29+
class Solution
30+
{
31+
public:
32+
/**
33+
* @brief Detects if a linked list contains a cycle
34+
*
35+
* This function uses Floyd's Cycle Detection Algorithm with two pointers:
36+
* - Tortoise (slow pointer): moves one step at a time
37+
* - Hare (fast pointer): moves two steps at a time
38+
*
39+
* If there's a cycle, the fast pointer will eventually catch up to the slow pointer.
40+
* If there's no cycle, the fast pointer will reach the end of the list.
41+
*
42+
* @param head Pointer to the head of the linked list
43+
* @return true if the linked list contains a cycle, false otherwise
44+
*
45+
* @note A cycle exists when a node's next pointer points to a previous node
46+
*
47+
* @example
48+
* List: 1->2->3->4->2 (cycle back to node 2)
49+
* Returns: true
50+
*
51+
* List: 1->2->3->4->NULL (no cycle)
52+
* Returns: false
53+
*
54+
* @complexity
55+
* Time Complexity: O(n) where n is the number of nodes in the list
56+
* Space Complexity: O(1) - only uses two pointer variables
57+
*
58+
* @algorithm Floyd's Cycle Detection Algorithm
59+
* 1. Initialize two pointers at head
60+
* 2. Move tortoise one step, hare two steps
61+
* 3. If they meet, cycle exists
62+
* 4. If hare reaches NULL, no cycle
63+
*/
64+
bool hasCycle(ListNode *head)
65+
{
66+
// Base case: empty list or single node cannot have a cycle
67+
if (!head || !head->next)
68+
return false;
69+
70+
// Initialize both pointers at the head of the list
71+
ListNode *hare = head; // Fast pointer (moves 2 steps)
72+
ListNode *tortoise = head; // Slow pointer (moves 1 step)
73+
74+
// Continue until hare reaches the end or finds a cycle
75+
while (hare && hare->next)
76+
{
77+
hare = hare->next->next; // Move hare 2 steps forward
78+
tortoise = tortoise->next; // Move tortoise 1 step forward
79+
80+
// If pointers meet, a cycle is detected
81+
if (hare == tortoise)
82+
return true;
83+
}
84+
85+
// Hare reached the end, no cycle found
86+
return false;
87+
}
88+
};

0 commit comments

Comments
 (0)