forked from codescoop/Play-with-Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
15_linked_list_floyds_cycle.cpp
714 lines (580 loc) · 17.5 KB
/
15_linked_list_floyds_cycle.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
/*
TOPIC: Cycle Detection & Removal (Floyd's Cycle)
(Detect and Remove Loop in a Linked List)
1. Check whether its contains cycle or not?
Eg: 1 -> 2 -> 3 -> 4 ->
^ |
| 5
| |
7 <- 6 <-
Approach:
- we will use the runner technique
- Slow pointer will take 1 step
- Fast pointer will take 2 step
- If slow & fast meets after some point, then linked list contains a cycle.
else
If fast reaches NULL & slow & fast doesn't meet, then linked list doesn't contain cycle.
_____________________
F___________|__________________ |
S___________|_______________ | |
| | | |
Eg: 1 -> 2 ->| 3 -> 4 -> | | |
| ^ | | | |
| | 5 | | |
| | | | | |
| 7 <- 6 <- | | |
| | | |
| S______| | |
|__________________| |
F____________|
2. Removal of cycle in Linked list (By Floyd's Approach)
Approach:
- we will use the runner technique
- Slow pointer will take 1 step
- Fast pointer will take 2 step
- If slow & fast meets after some point, then linked list contains a cycle.
- Now, take slow at the beginning of linked list & don't change the position of fast &
- both slow & fast pointer with 1 step
- Now, when they again meet.
That point is where linked list is connected (i.e the starting point of the circular loop)
|-------------
|------X------| \
\
SF__________________________ |
| |
Eg: 1 -> 2 -> 3 -> 4 -> | |
T T ^ | | |
| | | 5 | | Y
| | | | | |
| | 7 <- 6 <- | |
| | | |
| |________SF______| |
Z | |
| /
\ |--------/
\------------|
In the above example, 3 is the point where cycle is connected (i.e the starting point of the circular loop)
Note: Floyd proved that distance X & distance Z are same.
i.e X = Z
That's why we move both slow & fast pointer with 1 step after first meet.
So, that they can again meet at point 3 (i.e the starting point of the circular loop)
*/
#include <iostream>
using namespace std;
class Node{
public:
int data;
Node *next;
// constructor
Node(int d)
{
data = d;
next = NULL;
}
};
// pass a pointer by reference (because we want to make changes to the original head pointer)
void insertAtHead(Node *&head, int d)
{
if(head == NULL)
{
head = new Node(d);
return;
}
Node *n = new Node(d);
n->next = head;
head = n;
}
// function to find length of linked list
int length(Node*head)
{
int cnt = 0;
while(head != NULL)
{
cnt++;
head = head->next;
}
return cnt;
}
// function to insert data at last position of linked list
void insertAtTail(Node*&head, int data)
{
// corner case
if(head == NULL)
{
// Node *n = new Node(data);
// head = n;
head = new Node(data);
return;
}
Node *tail = head; // using temp pointer
// Moving head towards tail
while(tail->next != NULL)
{
tail = tail->next;
}
// create & attach new node
tail->next = new Node(data);
}
// function to insert data at middle of linked list
void insertAtMiddle(Node*&head, int data, int pos)
{
//corner case
if(head==NULL or pos==0)
{
insertAtHead(head, data);
return;
}
else if (pos >= length(head))
{
insertAtTail(head, data);
return;
}
// take pos-1 jumps
int i=1;
Node *temp = head;
while(i < pos)
{
temp = temp->next;
i++;
}
// create a new node
Node *n = new Node(data);
n->next = temp->next;
temp->next = n;
}
// pass by value (because we don't want to change the original head pointer)
void print(Node *head)
{
while(head != NULL)
{
cout << head->data << " -> ";
head = head->next;
}
// cout << endl;
}
// function to delete data at start of linked list
void deleteAtHead(Node*&head)
{
if(head == NULL)
{
return;
}
Node*temp = head->next; // temp pointer is static. So it will be deleted at the end of function call
delete head; // delete the Node whose address is stored in "head" pointer
head = temp;
}
// function to delete data at end of linked list
void deleteAtTail(Node*&head)
{
if(head == NULL)
{
return;
}
Node *tail = head;
Node *prev;
// moving towards the end of linked list
while(tail->next != NULL)
{
prev = tail;
tail = tail->next;
}
delete tail; // delete the Node whose address is stored in "tail" pointer
// delete prev->next; // prev->nex & tail are pointing towards same address
prev->next = NULL;
}
// function to delete data at end of linked list
void deleteAtMiddle(Node*&head, int pos)
{
if(head==NULL)
{
return;
}
else if(pos >= length(head))
{
return;
}
// jump towards pos
Node *temp = head;
Node *prev;
int i = 1;
while(i <= pos)
{
prev = temp;
temp = temp->next;
i++;
}
prev->next = temp->next;
delete temp; // delete the Node whose address is stored in "temp" pointer
}
// Search Operation
// Linear Search
bool search(Node*head, int key)
{
Node*temp = head;
while(temp != NULL)
{
if(temp->data == key)
{
return true;
}
temp = temp->next;
}
// if key not found
return false;
}
// Linear Search (recursively)
bool searchRecursive(Node*head, int key)
{
//base case
if(head == NULL)
{
return false;
}
//rec case
if(head->data == key)
{
return true;
}
return searchRecursive(head->next, key);
}
// Linked List User Input
void take_input(Node*&head)
{
//
}
// Linked List User Input
Node* take_input_2()
{
Node *head = NULL;
int data;
cin >> data;
while(data != -1)
{
insertAtHead(head, data);
// insertAtTail(head, data);
cin >> data;
}
return head;
/* NOTE: Since we are adding elements at "head" of linked list.
So, we will be getting linked list in the reverse order.
*/
}
// Linked List User Input [When taking Input from a file]
Node* take_input_file()
{
Node *head = NULL;
int data;
// cin >> data;
while(cin >> data)
// while(data != -1)
{
insertAtHead(head, data);
// cin >> data;
}
return head;
/* NOTE: Since we are adding elements at "head" of linked list.
So, we will be getting linked list in the reverse order.
*/
}
/*
- Using "<<" for getting output
Eg: cout << head;
This will print the address of head.
But we want to print entire linked list uing "cout".
So, we will use operator overloading.
NOTE: cout << head;
| |
ostream Node
object object
So, "<<" operator will take 2 parameter.
One is Ostream object (for "cout")
Second is Node object (for "head")
*/
// // Opertor Overloading (i.e for cases like: cout << head;)
// void operator<<(ostream &os, Node *head)
// {
// print(head);
// return;
// }
// Cascading of Operators (i.e for cases like: cout << head << head2;)
ostream& operator<<(ostream &os, Node *head) // Both Return & Pass are by Reference
{
print(head);
return os; // returning "cout" by reference
/*
=> cout << head << head2;
|-----------|
return cout
=> cout << head2;
|------------|
return cout
=> cout; [OUTPUT]
NOTE: We will get "cout; as output.
"cout;" is a valid statement in C++ & writing it doesnot do anything.
*/
}
// Cascading of Operators (i.e for cases like: cin >> head >> head2;)
istream& operator>>(istream &is, Node *&head)
{
head = take_input_2();
return is;
}
// reverse a linked list [Complexity: O(N) time + O(1) space]
void reverse(Node *&head)
{
Node *curr = head; // for current node
Node *prev = NULL; // for previous node
Node *nxt; // for next node
while(curr != NULL)
{
nxt = curr->next; // save the next node
curr->next = prev; // make the current node point to previous node
prev = curr; // update previous node (for the next iteration)
curr = nxt; // update current node (for the next iteration)
}
head = prev; // update head
}
// recursively reverse a linked list [Complexity: O(N) time + O(N) space]
Node* recursive_reverse(Node *head)
{
// smallest linked list or when actual linked list is null
if(head->next == NULL or head == NULL)
{
return head;
}
// rec case
Node *smallhead = recursive_reverse(head->next);
// Node *temp = smallhead; // Before Optimization [Complexity: O(N^2) time] i.e O(N) for both recursion & Loop
// while(temp->next != NULL)
// {
// temp = temp->next;
// }
Node *temp = head->next; // After Optimization [Complexity: O(N) time]
temp->next = head;
head->next = NULL;
return smallhead;
/*
// NOTE: This is also correct.
Node *smallhead = recursive_reverse(head->next);
head->next->next = head;
head->next = NULL;
return smallhead;
*/
}
// Finding the mid point of linked list
Node* midpoint(Node *head)
{
// linked list with 0 or 1 node
if(head == NULL or head->next == NULL)
{
return head;
}
Node *slow = head;
Node *fast = head->next;
while(fast != NULL and fast->next != NULL) // checking 2 cases for fast pointer bcz fast takes 2 steps
{
fast = fast->next->next; // moving 2 steps
slow = slow->next; // moving 1 step
}
return slow; // returning the position where slow pointer stops (i.e mid point)
}
// Finding the kth node from the end of linked list
Node* kthNode(Node *head, int k)
{
if(head == NULL)
{
return head;
}
Node *fast = head;
Node *slow = head;
while(fast->next != NULL)
{
// moving k steps
if(k-1)
{
fast = fast->next;
k--;
}
else
{
// moving 1 steps
fast = fast->next;
slow = slow->next;
}
}
return slow;
}
// funtion to merge two linked lists
Node* merge(Node* a, Node* b)
{
// base case
if(a == NULL)
{
return b;
}
if(b == NULL)
{
return a;
}
// rec case
Node *c; // take a new head pointer
if(a->data < b->data)
{
c = a;
c->next = merge(a->next, b); // return, merge of "pending a" with "b"
}
else{
c = b;
c->next = merge(a, b->next); // return, merge of "a" with "pending b"
}
return c; // return the head pointer
/*
Eg: a : 1 -> 2 -> 4 -> 8 -> 9 -> NULL
b : 0 -> 5 -> 7 -> NULL
Now, merge(a,b)
c : 0 ->
| 1 -> 2 -> 4 -> 8 -> 9 -> NULL // a
| 5 -> 7 -> NULL // pending of b
c : 0 -> 1 ->
| 2 -> 4 -> 8 -> 9 -> NULL // pending of a
| 5 -> 7 -> NULL // b
c : 0 -> 1 -> 2 ->
| 4 -> 8 -> 9 -> NULL // pending of a
| 5 -> 7 -> NULL // b
c : 0 -> 1 -> 2 -> 4 ->
| 8 -> 9 -> NULL // pending of a
| 5 -> 7 -> NULL // b
c : 0 -> 1 -> 2 -> 4 -> 5 ->
| 8 -> 9 -> NULL // a
| 7 -> NULL // pending of b
c : 0 -> 1 -> 2 -> 4 -> 5 -> 7 ->
| 8 -> 9 -> NULL // a
| NULL // pending of b
c : 0 -> 1 -> 2 -> 4 -> 5 -> 7 -> 8 -> 9 -> NULL
*/
}
// merge sort in linked list [Time Complexity: NLogN (same as in array)]
Node* mergeSort(Node* head)
{
// base case: For 0 or 1 Node in linked list
if(head == NULL or head->next == NULL)
{
return head;
}
// res case:
// 1. divide/break
Node *mid = midpoint(head); // Time Complexity: O(N) (Note: array takes O(1) to find midpoint)
Node *a = head; // "a" points at beginning of Linked list
Node *b = mid->next; // "b" points next to "mid node" of Linked list (till end)
mid->next = NULL; // to make "a" points till "mid" (i.e to remove linked nodes after "mid")
// 2. Recursive sort the two parts
a = mergeSort(a);
b = mergeSort(b);
// 3. merge
Node* newhead = merge(a,b);
return newhead;
/*
Eg: a(head) b(mid->next)
| |
=> 1 -> 0 -> 3 -> 4
|
mid
=> After, mid->next = NULL;
=> 1 -> 0 3 -> 4
| |
a b
*/
}
// fuction to detect loop/cycle in linked list
bool detectCycle(Node *head)
{
Node *slow = head;
Node *fast = head;
while(fast != NULL and fast->next != NULL)
{
fast = fast->next->next; // fast takes 2 steps
slow = slow->next; // slow takes 1 step
if(fast==slow) // if fast == slow before reaching NULL
{
return true;
}
}
return false; // if slow & fast doesn't meet
}
// fuction to detect & remove loop in linked list
bool detectAndRemoveCycle(Node *head)
{
bool isCyclic = false;
Node *slow = head;
Node *fast = head;
while(fast != NULL or fast->next != NULL)
{
fast = fast->next->next; // fast takes 2 steps
slow = slow->next; // slow takes 1 step
if(fast == slow) // if fast == slow before reaching NULL
{
slow = head; // slow points at beginning of Liked List
isCyclic = true;
break;
}
}
if(isCyclic) // if loop found
{
Node *prev;
while(fast != slow)
{
prev = fast;
fast = fast->next; // fast takes 1 step
slow = slow->next; // slow takes 1 step
}
prev->next = NULL;
}
return isCyclic;
}
int main()
{
Node *head = NULL;
cout << "Linked List: ";
insertAtHead(head, 7);
insertAtHead(head, 6);
insertAtHead(head, 5);
insertAtHead(head, 4);
insertAtHead(head, 3);
insertAtHead(head, 2);
insertAtHead(head, 1);
print(head);
cout << "[Last Node -> 3rd Node]" << endl;
// cout << "head: " << head->next->next->next->next->next->next->data << endl;
// cout << "head: " << head->next->next->data << endl;
// Creating a Loop/Cycle in linked list (for testing)
head->next->next->next->next->next->next->next = head->next->next; // "Last Node" pointing towards "3rd Node"
// Detect Loop in linked
bool status = detectCycle(head);
if(status)
{
cout << "Loop Found " << endl;
}
else
{
cout << "No Loop Found" << endl;
}
// Detect & Remove Loop in linked list
status = detectAndRemoveCycle(head);
if(status)
{
cout << "Linked List [after removing loop]: ";
print(head);
cout << endl;
}
return 0;
}
/*
OUTPUT:
Case 1: [Only Detect Loop]
Linked List: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> [Last Node -> 3rd Node]
Loop Found
Case 2: [Detect & Remove Loop]
Linked List : 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> [Last Node -> 3rd Node]
Loop Found
Linked List [after removing loop] : 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7
*/