-
Notifications
You must be signed in to change notification settings - Fork 617
/
RemoveLoopFromLinkedList.cpp
195 lines (138 loc) · 4.1 KB
/
RemoveLoopFromLinkedList.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
/* Remove Loop from the linked list. Linked list has to be taken as an input from the user*/
#include<bits/stdc++.h>
using namespace std;
struct node {
int data;
struct node * next;
};
void print_list(struct node * head) {//printing each data using iteration
while (head) {
cout << head -> data << " ";
head = head -> next;
}
cout << endl;
}
void insert_front(struct node ** head, int data) {//allocating memory to each data being taken as an input
struct node * new_node = NULL;
new_node = (struct node * ) malloc(sizeof(struct node));
if (new_node == NULL) {
cout << "\nUnable to allocate memory\n";
}
new_node -> data = data;
new_node -> next = * head;
* head = new_node;
}
void create_loop(struct node * head) { //creating a loop if the loop does not exist
struct node * temp = head;
while (temp -> next)
temp = temp -> next;
temp -> next = head -> next; //linking ladt node and second node
}
void print_loop(struct node * head) {//printing each head data using iteration
int n = 25;
while (n--) {
cout << head -> data << " ";
head = head -> next;
}
cout << endl;
}
void detect_loop(struct node * head) {
struct node * slow = head;
struct node * fast = head;
while (slow && fast -> next && fast -> next -> next) {
if ((slow == fast -> next) || (slow == fast -> next -> next)) {
cout << "\nLinked list has a loop\n";
return;
}
slow = slow -> next;
fast = fast -> next -> next;
}
cout << "\nLinked list does not have any loop\n";
}
void remove_loop(struct node * head, struct node * loop_node) {
struct node * near = head;
struct node * far = head;
struct node * ptr = loop_node;
struct node * prev = NULL;
while (ptr -> next != loop_node) {
ptr = ptr -> next;
far = far -> next;
}
prev = far;
far = far -> next;
while (near != far) {
prev = far;
far = far -> next;
near = near -> next;
}
prev -> next = NULL;
}
void detect_and_remove_loop(struct node * head) {
struct node * slow = head;
struct node * fast = head;
while (slow && fast -> next && fast -> next -> next) {
if ((slow == fast -> next) || (slow == fast -> next -> next)) {
cout << "\nLinked list has a loop\n";
remove_loop(head, slow);
return;
}
slow = slow -> next;
fast = fast -> next -> next;
}
cout << "\nLinked list does not have any loop\n";
}
int main() {
int n, i, data;
struct node * head = NULL;
cout << "\nEnter number of elements :";
cin >> n;
cout << "\nEnter the elements :";
for (i = 0; i < n; i++) {
cin >> data;
insert_front( & head, data);
}
cout << "\nThe Linked List is : ";
print_list(head);
detect_loop(head);
cout << "\nCreating loop…\n";
create_loop(head);
cout << "\nPrinting list with loop\n";
print_loop(head);
cout << "\nRemoving loop…\n";
detect_and_remove_loop(head);
cout << "\nList after removing loop:\n";
print_list(head);
}
/*
---------------------------------------------------------
SAMPLE TEST CASE 1:
INPUT:
Enter number of elements :5
Enter the elements :50 40 12 10 9
OUTPUT:
The Linked List is : 9 10 12 40 50
Linked list does not have any loop
Creating loop…
Printing list with loop
9 10 12 40 50 10 12 40 50 10 12 40 50 10 12 40 50 10 12 40 50 10 12 40 50
Removing loop…
Linked list has a loop
List after removing loop:
9 10 12 40 50
-------------------------------------------------
SAMPLE TEST CASE 2:
Enter number of elements :6
Enter the elements :1 2 3 4 5 6
The Linked List is : 6 5 4 3 2 1
Linked list does not have any loop
Creating loop…
Printing list with loop
6 5 4 3 2 1 5 4 3 2 1 5 4 3 2 1 5 4 3 2 1 5 4 3 2
Removing loop…
Linked list has a loop
List after removing loop:
6 5 4 3 2 1
-----------------------------------------------------
TIME COMPLEXITY: O(n) (n is number of elements in the linked list)
SPACE COMPLEXITY: O(1)
*/