-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path61.LinkedList_findingLoop.cpp
68 lines (58 loc) · 1.25 KB
/
61.LinkedList_findingLoop.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
//finding loop in linked list
#include<iostream>
using namespace std;
struct node{
int data;
node* next;
};
node * first = new node;
bool isLoop(node* p){
node* ptr1, *ptr2;
ptr1 = ptr2 = p;
do{
//iterations ptr1 by 1 iteration
ptr1 = ptr1->next;
//iterate ptr2 by 2 iterations
ptr2 = ptr2->next;
ptr2 = (ptr2==NULL)?ptr2:ptr2->next;
if(ptr1 == ptr2){
return true;
}
}while(ptr1 && ptr2);
return false;
}
void create(node* p,int* arr,int num, int create_loop){
node* temp;
node* loop;
p->data = arr[0];
for(int i=1;i<num;i++){
temp = new node;
temp->data = arr[i];
temp->next = NULL;
if(create_loop == i){
loop = temp;
}
p->next = temp;
p = p->next;
}
if(create_loop){
p->next = loop;
}
}
void display(node* p){
while(p){
cout<<p->data<<" ";
p = p->next;
}
cout<<endl;
}
int main(){
int arr[] = {1,2,3,4,5,6,7};
create(first,arr,7,3);
//display(first);
cout<<"Loop: "<<isLoop(first);
node* second = new node;
int arr2[] = {1,2,3,4,5,6,7,8,9,10};
create(second,arr2,10,0);
cout<<"Loop: "<<isLoop(second);
}