-
Notifications
You must be signed in to change notification settings - Fork 0
/
Linked_list_cycle_striver.c++
47 lines (44 loc) · 1.29 KB
/
Linked_list_cycle_striver.c++
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
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool hasCycle(ListNode *head) {
// the runner's algorithm is used for the finding of middle of linked list and this floyd detection algo whihc is
// used for the finding of cycle in linked list
if(head==NULL || head->next==NULL){
return false;
}
ListNode*p=head;
ListNode*q=head;
// jo aage hai usko 2 -2 badhauynga and jo peeche hai usko 1 ek badaunga;
// its optimised beacuse it doesn't take any extra space
// otherwise we can make it using maps
while(p!=NULL and q!=NULL){
if(p->next!=NULL){
p=p->next;
}else{
break;
}
if(q->next!=NULL){
q=q->next;
if(q->next!=NULL){
q=q->next;
if(p==q){
return true;
}
}else{
break;
}
}else{
break;
}
}
return false;
}
};