-
Notifications
You must be signed in to change notification settings - Fork 93
/
DetectLoopinLinkedlist.cpp
61 lines (48 loc) · 1.1 KB
/
DetectLoopinLinkedlist.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
54
55
56
57
58
59
60
61
//C++ program to detect loop in linkedlist using floyd's cycle finding algorithm
#include <bits/stdc++.h>
using namespace std;
/* Link list node */
class Node {
public:
int data;
Node* next;
};
void push(Node** head_ref, int new_data)
{
Node* new_node = new Node();
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int detectLoop(Node* head)
{
Node *fast_pointer = head;
Node *slow_pointer = head;
while(fast_pointer && fast_pointer->next)
{
slow_pointer = slow_pointer->next;
fast_pointer = fast_pointer->next->next;
if(fast_pointer == slow_pointer)
{
return 1;
}
}
return 0;
}
int main()
{
/* Start with the empty list */
Node* head = NULL;
push(&head, 20);
push(&head, 4);
push(&head, 15);
push(&head, 15);
push(&head, 10);
/* Create a loop for testing */
head->next->next->next = head;
if (detectLoop(head))
cout << "Loop found";
else
cout << "No Loop";
return 0;
}