forked from iUttamRao/Hacktoberfest-22
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetectLoop.cpp
More file actions
165 lines (140 loc) · 3.36 KB
/
Copy pathdetectLoop.cpp
File metadata and controls
165 lines (140 loc) · 3.36 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include<iostream>
#include<map>
using namespace std;
// implementation of node
class Node{
public:
int data;
Node* next;
Node(int data){ //constructor
this->data = data;
this->next = NULL;
}
};
//singly linked list
//insert at head
void insertAtHead(Node* &head, int d){
//create a new node
Node *temp = new Node(d);
temp->next = head;
head = temp;
}
//insert at tail/end
void insertAtTail(Node* &tail, int d){
Node* temp = new Node(d);
tail->next = temp;
tail = temp;
}
//insert at middle
void insertAtPosition(Node* &tail,Node* &head,int pos,int d){
//to insert at start
if(pos==1){
insertAtHead(head,d);
return;
}
Node *temp = head;
int cnt=1;
while(cnt<pos-1){
temp = temp->next;
cnt++;
}
if(temp->next == NULL){
insertAtTail(tail,d);
return;
}
//create a node for d
Node* nodeToInsert = new Node(d);
nodeToInsert->next = temp->next;
temp->next = nodeToInsert;
}
//Detect a cycle in list by using map
// bool isDetect(Node* head){
// if(head == NULL){
// return false;
// }
// map<Node* ,bool>visited;
// Node* temp = head;
// while(temp!=NULL){
// //cycle is present
// if(visited[temp] == true){
// cout<<"Present at "<<temp->data<<endl;
// return true;
// }
// visited[temp] = true;
// temp= temp->next;
// }
// return false;
// }
//time complexity->O(n)
//space complexity->O(n)
//Detect the cycle using floyd's detection algo
Node* floydDetectLoop(Node* head){
if(head == NULL ){
return NULL;
}
Node* fast = head;
Node* slow = head;
while(slow!=NULL && fast!=NULL){
fast = fast->next;
if(fast!=NULL){
fast = fast->next;
}
slow = slow->next;
if(slow == fast){
cout<<"Present at "<<slow->data<<endl;
return slow;
}
}
return NULL;
}
//Space Complexity = O(1)
//Time Complexity = o(n)
//traverse linked list
void print(Node* &head){
Node* temp = head;
while (temp!=NULL)
{
cout<<temp->data<<" ";
temp = temp->next;
}
cout<<endl;
}
int main(){
//created a new node
Node* node1 = new Node(10);
// cout<<node1->data<<endl;
// cout<<node1->next<<endl;
//head pointed to node1
Node* head = node1;
//tail pointed to node1
Node* tail = node1;
print(head);
//insertAtHead(head,12);
insertAtTail(tail,12);
print(head);
insertAtTail(tail,45);
print(head);
insertAtTail(tail,95);
print(head);
//insertAtPosition(head,3,5);
//insertAtPosition(head,1,34);
insertAtPosition(tail,head,5,22);
print(head);
// cout<<head->data<<endl;
// cout<<tail->data<<endl;
Node* temp = head;
tail->next = temp->next;
// if(isDetect(head)){
// cout<<"Cycle is present"<<endl;
// }
// else{
// cout<<"Cycle is not present"<<endl;
// }
if(floydDetectLoop(head)!=NULL){
cout<<"Cycle is present"<<endl;
}
else{
cout<<"Cycle is not present"<<endl;
}
return 0;
}