-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9_16_removeCyceInLL.cpp
298 lines (278 loc) · 5.89 KB
/
9_16_removeCyceInLL.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include <bits/stdc++.h>
using namespace std;
class Node
{
public:
int data;
Node *next;
// constructor
Node(int data)
{
this->data = data;
this->next = nullptr;
}
};
void print(Node *head)
{
cout << head->data << " ";
Node *temp = head->next;
while (temp != head)
{
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
// take input for singly circular LL
Node *takeInput()
{
// we can not ask size as linked list is dynamic
// so will take input as linked list node
// untill -1 is given.
cout << "enter your linked list: " << endl;
int data;
cin >> data;
Node *head = nullptr;
Node *tail = nullptr;
while (data != -1)
{
Node *newNode = new Node(data);
if (head == nullptr)
{
head = newNode;
tail = newNode;
}
else
{
// link newNode with previos node
tail->next = newNode;
// update tail now
tail = newNode;
// or, tail = tail->next;
}
// further input for nodes
cin >> data;
}
// attach tail with head;
tail->next = head;
// returning head of the linked list
return head;
}
int length(Node *head)
{
// in case of empty node
if (head == nullptr)
{
return 0;
}
Node *temp = head->next;
int ans = 1;
while (temp != head)
{
ans++;
temp = temp->next;
}
return ans;
}
Node *insertNode(Node *head, int index, int data)
{
// inserting in 0th index. means as a head of the LL
if (index == 0)
{
Node *newNode = new Node(data);
newNode->next = head;
// Node to traverse till the last node
Node *temp = head->next;
while (temp->next != head)
{
temp = temp->next;
}
// attach temp with newNode
temp->next = newNode;
// update head
head = newNode;
return head;
}
// check for valid index
int len = length(head);
if (index >= len)
{
return head;
}
// insert at middle of the LL
int i = 0;
Node *temp = head;
while (i < index - 1)
{
i++;
temp = temp->next;
}
Node *newNode = new Node(data);
Node *a = temp->next;
temp->next = newNode;
newNode->next = a;
return head;
}
// delete node with ith index
Node *deleteNode(Node *head, int i)
{
// deleting head node
if (i == 0)
{
Node *temp = head;
head = head->next;
// traverse to find last node
Node *last = head;
while (last->next != temp)
{
last = last->next;
}
last->next = head;
delete temp;
return head;
}
// check for valid index
int len = length(head);
if (i >= len)
{
return head;
}
// deleting mid nodes
int cnt = 1;
Node *temp = head;
while (cnt < i)
{
temp = temp->next;
cnt++;
}
Node *a = temp->next;
temp->next = head;
delete a;
return head;
}
// detect cycle in a linked list
// T:O(n), S:O(n)
bool detectCycle(Node *head)
{
if (head == nullptr)
{
return false;
}
Node *temp = head;
// map to store node and check if node is visited
map<Node *, bool> visited;
while (temp != nullptr)
{
// if temp is already visited, then there is cycle
if (visited[temp] == true)
{
return true;
}
// mark temp as visited
visited[temp] = true;
temp = temp->next;
}
return false;
}
// detect cycle using floyd's formula
// T:O(n), S:O(1)
// here, we are returning a node inside the loop
Node *floydDetectCycle(Node *head)
{
if (head == nullptr)
{
// return false;
return nullptr;
}
Node *slow = head;
Node *fast = head;
while (slow != nullptr && fast != nullptr)
{
// run fast as twice as slow
fast = fast->next;
// check if fast has become null
if (fast != nullptr)
{
fast = fast->next;
}
// update slow
slow = slow->next;
// if slow and fast are same, then there is cycle
if (fast == slow && fast != nullptr && slow != nullptr)
{
// return true;
return slow;
}
}
// function comes out of the loop
// so no loop is there
// return false;
return nullptr;
}
// function to find statring node of a loop
Node *startingNode(Node *head)
{
// check if cycle exists in the list
Node *slow = floydDetectCycle(head);
if (slow == nullptr)
{
// there is no cycle
return nullptr;
}
else
{
Node *intersection = slow;
slow = head;
while (slow != intersection)
{
slow = slow->next;
intersection = intersection->next;
}
return slow;
}
}
// function to remove cycle within a LL
void removeCycle(Node *head)
{
if (head == nullptr)
{
return;
}
// get the first node of the cycle
Node *startOfLoop = startingNode(head);
Node *temp = startOfLoop;
while (temp->next != startOfLoop)
{
temp = temp->next;
}
// upagrade temp's next as null to remove cycle
temp->next = nullptr;
return;
}
int main()
{
Node *head = takeInput();
print(head);
Node *a = new Node(10);
Node *b = new Node(20);
a->next = b;
Node *start = startingNode(head);
if (start == nullptr)
{
cout << "no loop is there" << endl;
}
else
{
cout << "loop starts with node " << start->data << endl;
}
removeCycle(head);
if (detectCycle(head) == true)
{
cout << "cycle is still there" << endl;
}
else
{
cout << "cycle has been removed" << endl;
}
return 0;
}