-
Notifications
You must be signed in to change notification settings - Fork 93
/
FloydsCycleDetectionAlgorithm.cpp
52 lines (43 loc) · 1.1 KB
/
FloydsCycleDetectionAlgorithm.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//Floyds Cycle Detection Algorithm
//Using the two pointer approach (Slow and Fast Pointer)
#include<bits/stdc++.h>
using namespace std;
struct Node
{
int data;
Node *next;
Node(int x)
{
data=x;
next=NULL;
}
};
//Function to detect if a Cycle is present or not
//Using Slow and Fast Pointers
bool detectLoop(Node *head)
{
Node *slowPointer = head;
Node *fastPointer = head;
while (slowPointer != NULL && fastPointer != NULL && fastPointer->next != NULL) {
slowPointer = slowPointer->next;
fastPointer = fastPointer->next->next;
if (slowPointer == fastPointer)
return 1;
}
return 0;
}
int main()
{
//A Circular Linked List is taken as an example
Node *head=new Node(10);
Node *temp1=new Node(20);
Node *temp2=new Node(30);
head->next = temp1;
temp1->next=temp2;
temp2->next=head;
if(detectLoop(head) == 1)
cout<<"Cycle is present"<<endl;
else
cout<<"Cycle is not present"<<endl;
return 0;
}