-
Notifications
You must be signed in to change notification settings - Fork 0
/
Detect_Loop_in_Singly_linkedlist.cpp
53 lines (45 loc) · 1.04 KB
/
Detect_Loop_in_Singly_linkedlist.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
53
/* This program uses the Floyd's Cycle-Finding Algorithm */
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int data;
Node *next;
};
void push(Node **head_ref, int new_data){
Node *newNode = new Node();
newNode->data = new_data;
newNode->next = (*head_ref);
(*head_ref) = newNode;
}
int detectLoop(Node *list){
Node *slow_ptr = list, *fast_ptr = list;
while(slow_ptr && fast_ptr && fast_ptr->next){
slow_ptr = slow_ptr->next;
fast_ptr = fast_ptr->next->next;
if(slow_ptr == fast_ptr){
return 1;
}
}
return 0;
}
int main(){
Node *head = NULL;
int ele;
cout << "Enter elements or '-1' to terminate: ";
cin >> ele;
while(ele != -1){
push(&head, ele);
cin >> ele;
}
cout << endl;
/* Create a Sample loop here
* head->next->next = head;
*/
if(detectLoop(head)){
cout << "Loop detected" << endl;
} else{
cout << "No loop detected" << endl;
}
return 0;
}