-
Notifications
You must be signed in to change notification settings - Fork 105
/
loopdetection.cpp
79 lines (63 loc) · 1.97 KB
/
loopdetection.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*EXPLANATION
Floyd’s Cycle-Finding Algorithm // fast slow approach // 2 pointers // "tortoise and the hare algorithm"
Approach: This is the fastest method and has been described below:
Traverse linked list using two pointers.
Move one pointer(slow_p) by one and another pointer(fast_p) by two.
If these pointers meet at the same node then there is a loop. If pointers do not meet then linked list doesn’t have a loop.*/
#include<bits/stdc++.h>
using namespace std;
class node{
public:
int data;
node*next;
};
void push(node** head_ref, int new_data)
{
/* allocate node */
node* new_node = new node();
/* put in the data */
new_node->data = new_data;
/* link the old list off the new node */
new_node->next = (*head_ref);
/* move the head to point to the new node */
(*head_ref) = new_node;
}
bool hasCycle(node *head) {
// if head is NULL then return false;
if(head == NULL)
return false;
// making two pointers fast and slow and assignning them to head
node *fast = head;
node *slow = head;
// till fast and fast-> next not reaches NULL
// we will increment fast by 2 step and slow by 1 step
while(fast != NULL && fast ->next != NULL)
{
fast = fast->next->next;
slow = slow->next;
// At the point if fast and slow are at same address
// this means linked list has a cycle in it.
if(fast == slow)
return true;
}
// if traversal reaches to NULL this means no cycle.
return false;
}
/* Driver code*/
int main()
{
/* Start with the empty list */
node* head = NULL;
push(&head, 20);
push(&head, 4);
push(&head, 15);
push(&head, 10);
/* Create a loop for testing */
head->next->next->next->next = head;
if (hasCycle(head))
cout << "Loop found";
else
cout << "No Loop";
return 0;
}
//output will be loop found