forked from kishanrajput23/Code-Linting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComplete LinkedList
306 lines (283 loc) · 6.8 KB
/
Complete LinkedList
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
#include <bits/stdc++.h>void insertAtTail(Node *&head, int val)
{ /*TIME COMP..... O(N) ,
But if they have given tail instead of head then
we don't have to traverse
So, the TIME COMPLE... at that time is O(1)*/
Node *temp = head;
while (temp->next != NULL)
{
temp = temp->next;
}
Node *newNode = new Node(val);
temp->next = newNode;
}
using namespace std;
class Node
{
public:
int value;
Node *next;
Node(int data)
{
value = data;
next = NULL;
}
};
// To insert Node at head
void insertAtHead(Node *&head, int val)
{ // TIME COMPLE.. O(1)
Node *newNode = new Node(val);
newNode->next = head;
head = newNode;
}
// To insert Node at tail
// To insert Node at arbitrary Position (at given index)
void insertAtArbitrary(Node *&head, int index, int val)
{
if (index == 0)
{
insertAtHead(head, val);
return;
}
Node *newNode = new Node(val);
Node *temp = head;
while ((index--) - 1)
{
temp = temp->next;
}
newNode->next = temp->next;
temp->next = newNode;
}
// To Update value at a position
void updateAtPosition(Node *&head, int index, int val)
{
Node *temp = head;
while (index--)
{
temp = temp->next;
}
temp->value = val;
}
// Deleting the Node the at the start
void deleteAtHead(Node *&head)
{
Node *temp = head;
head = head->next;
free(temp);
}
// Deleting the Node the at the end
void deleteAtTail(Node *&head)
{
Node *second_last = head;
while (second_last->next->next != NULL)
{
second_last = second_last->next;
}
Node *temp = second_last->next;
second_last->next = NULL;
free(temp);
}
// Deleting the Node at the Kth Position
void deleteAtGivenPosition(Node *&head, int index)
{
if (index == 0)
{
deleteAtHead(head);
}
Node *previous = head;
while ((index--) - 1)
{
previous = previous->next;
}
Node *temp = previous->next;
previous->next = previous->next->next;
free(temp);
}
// Delete every alternative element starting from 2nd element
void deleteAlternativeElement(Node *&head)
{
Node *current = head;
Node *temp;
while (current != NULL && current->next != NULL)
{
temp = current->next;
current->next = current->next->next;
free(temp);
current = current->next;
}
}
// Delete all duplicates from the sorted Linkedlist
void deleteDupliatedInSortedLL(Node *&head)
{
Node *current = head;
Node *temp;
while (current->next != NULL)
{
if (current->value != current->next->value)
{
current = current->next;
}
else
{
temp = current->next;
current->next = current->next->next;
free(temp);
}
}
}
void DeleteDuplictedNodesUsingMaamMethod(Node *&head)
{
Node *current = head;
while (current)
{
while (current->next && current->value == current->next->value)
{
Node *temp = current->next;
current->next = current->next->next;
free(temp);
}
current = current->next;
}
}
// Printing Reverse LinkedList
void PrintingReverseLL(Node *head)
{
// Base case
if (head == NULL)
return;
// Recursive case
PrintingReverseLL(head->next);
cout << head->value << "-->";
}
// Reverse LinkedList normally and using Recursion
Node *ReverseLL(Node *&head)
{
Node *previous = NULL;
Node *current = head;
// move all three pointer one step ahead
while (current != NULL)
{
Node *nextptr = current->next;
current->next = previous;
previous = current;
current = nextptr;
}
// now the loop end So, my previous pointer is my last node which is new node .
Node *new_node = previous;
return new_node;
}
Node *ReverseLLusingRecursion(Node *&head)
{
// Base condition
if (head == NULL || head->next == NULL)
{
return head;
}
// recursion condition
Node *new_head = ReverseLLusingRecursion(head->next);
head->next->next = head;
head->next = NULL;
return new_head;
}
// Reverse nodes Kth at a time
Node *ReverseByKthNode(Node *head, int number)
{
Node *previous = NULL;
Node *current = head;
int count = 0; // for counting first k nodes
while ((count < number) && current != NULL)
{ // reversing first k nodes
Node *nextptr = current->next;
current->next = previous;
previous = current;
current = nextptr;
count++;
}
// current pointer will give us (k+1)node.
if (current != NULL)
{
Node *new_head = ReverseByKthNode(current, number); // recursive call
head->next = new_head;
}
return previous; // previous will give the new head to the connected linkedlist
}
void deleteNodewithvalue(Node *&head, int value)
{
Node *previous = NULL;
Node *current = head;
while (current != NULL)
{
if (current->value == value)
{
previous->next = current->next;
break;
}
previous = current;
current = current->next;
}
}
// To display the linkedList
void display(Node *head)
{
Node *temp = head;
while (temp != NULL)
{
cout << temp->value << "-->";
temp = temp->next;
}
cout << "NULL" << endl;
}
int main()
{
Node *head = NULL;
cout << "insertAtHead-->";
insertAtHead(head, 2);
display(head);
cout << "insertAtHead-->";
insertAtHead(head, 1);
display(head);
cout << "insertAtTail-->";
insertAtTail(head, 3);
display(head);
cout << "insertAtArbitrary-->";
insertAtArbitrary(head, 3, 4);
display(head);
cout << "UpdateAtPosition-->";
updateAtPosition(head, 3, 5);
display(head);
cout << "insertAtTail-->";
insertAtTail(head, 5);
insertAtTail(head, 5);
insertAtTail(head, 5);
insertAtTail(head, 6);
insertAtTail(head, 6);
insertAtTail(head, 7);
insertAtTail(head, 7);
display(head);
// cout << "deleteAtGivenPosition-->";
// deleteAtGivenPosition(head, 2);
// display(head);
// cout << "deleteAtHead-->";
// deleteAtHead(head);
// display(head);
// cout << "deleteAtTail-->";
// deleteAtTail(head);
// display(head);
// }
// cout<<"DeleteAlternativeElement-->";
// deleteAlternativeElement(head);
cout << "deleteDupliatedInSortedLL-->";
deleteDupliatedInSortedLL(head);
display(head);
// cout << "Printing Reverse LinkedList-->";
// PrintingReverseLL(head);
// cout << endl;
// cout << "LL Reversed-->";
// head = ReverseLLusingRecursion(head);
// display(head);
head = ReverseByKthNode(head, 2);
display(head);
cout << "here";
deleteNodewithvalue(head, 5);
display(head);
}