-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedList.cpp
391 lines (353 loc) · 8.26 KB
/
LinkedList.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
// LinkedList
using namespace std;
#include <bits/stdc++.h>
struct Node
{
int data;
Node *next;
Node()
{
data = 0;
next = nullptr;
}
Node(int val)
{
data = val;
next = nullptr;
}
Node(int val, Node *node)
{
data = val;
next = node;
}
};
void printList(Node *head)
{
while (head)
{
cout << head->data << "->";
head = head->next;
}
cout << "Null/n";
}
/*
Yes! Node*& head is a reference to a pointer. Let's break it down:
Understanding Node*& head
Node* head → A pointer to a Node (stores the address of a node).
Node*& head → A reference to a pointer to a Node.
Why Use Node*& head?
If we pass a pointer normally (Node* head), it gets copied, so changes inside the function won't affect the original pointer.
By passing a reference to a pointer (Node*& head), we allow the function to modify the original pointer.
*/
void insertAtTail(Node *&head, int val)
{
Node *newNode = new Node(val);
if (!head)
{
head = newNode;
return;
}
Node *temp = head;
while (temp->next)
temp = temp->next; // while(temp->next) and while(temp->next != nullptr) are functionally the same
temp->next = newNode;
}
bool hasCycle(Node *node)
{
Node *slow = node;
Node *fast = node;
while (fast && fast->next)
{
slow = slow->next;
fast = fast->next->next;
if (slow == fast)
return true;
}
return false;
}
// works acc to Floyd’s Cycle Detection Algorithm.
void removeCycle(Node *head)
{
Node *slow = head;
Node *fast = head;
do
{
if (fast || fast->next)
return;
slow = slow->next;
fast = fast->next->next;
} while (slow != fast);
// fast and slow are at the same node
slow = head;
while (slow->next != fast->next)
{
slow = slow->next;
fast = fast->next;
}
fast->next = nullptr;
}
// so acc to floyd's cycle detection, the node at which
// the cycle is detected can be used to break the cycle,
// by keeping one pointer at the point and taking the next
// one to the head and looping till they are equal and then
// changing the node's next value that was kept at the cycle
// detection node to nullptr
// now delete(by value) and reverse by myself
void deleteNode(Node *&head, int val)
{
if (head == nullptr)
return;
if (head->data == val)
{
Node *temp = head;
head = head->next;
delete temp;
return;
}
Node *current = head;
while (current->next)
{
if (current->next && current->next->data == val)
{
Node *temp = current->next;
current->next = current->next->next;
delete temp;
}
current = current->next;
}
}
void ReverseNode(Node *&head)
{
Node *prev = nullptr;
Node *current = head;
Node *temp;
while (current)
{
temp = current->next;
current->next = prev;
prev = current;
current = temp;
}
head = prev;
}
// done - print, reverse, cycle detection and removal using floyd's principle, insertion, deletion.
// Following will be some good questions from AI bot
/*
1. Remove Duplicates from an Unsorted Linked List // using hash
2. Reverse a Linked List in Groups of Size K
3. Palindrome Linked List -
4. Intersection Point of Two Linked Lists
5. Add Two Numbers Represented by Linked Lists
6. Rotate a Linked List by K places
7. Use a linked list to represent a path in a grid-based game.
8. Recursive solution of LinkedLists
*/
#include <iostream>
#include <unordered_set>
void RemoveDuplicates(Node *head)
{
if (head == nullptr)
return;
unordered_set<int> seen;
Node *current = head;
Node *prev = nullptr;
while (current->next)
{
if (seen.find(current->data) != seen.end())
{
prev->next = current->next;
delete current;
current = prev;
}
else
{
seen.insert(current->data);
prev = current;
current = current->next;
}
}
}
Node *ReverseK(Node *head, int k)
{
if (head == nullptr || k)
return head;
Node *dummy = new Node();
dummy->next = head;
Node *prev = dummy, *curr = dummy, *next = nullptr;
int length = 0;
while (curr->next != nullptr)
{
curr = curr->next;
length++;
}
while (length >= k)
{
curr = prev->next;
next = curr->next;
for (int i = 0; i < k; i++)
{
curr->next = next->next;
next->next = prev->next;
prev->next = next;
next = curr->next;
}
prev = curr;
length -= k;
}
return dummy->next;
}
void Palindrome(Node *head)
{
if (head == nullptr)
return;
// find the middle of the list, reverse it then compare the two halves
}
void Intersection(Node *head)
{ // easy
if (head == nullptr)
return;
}
Node *addNumbers(Node *l1, Node *l2)
{
Node *dummy = new Node();
Node *curr = dummy;
int carry = 0;
while (l1 || l2 || carry != 0)
{
int sum = (l1 ? l1->data : 0) + (l2 ? l2->data : 0) + carry;
carry = sum / 10;
curr->next = new Node(sum % 10);
curr = curr->next;
if (l1)
l1 = l1->next;
if (l2)
l2 = l2->next;
}
return dummy->next;
}
void RotateK(Node *head, int k)
{
if (head == nullptr || k == 0)
return;
int length = 0;
Node *tail = head;
while (tail->next)
{
tail = tail->next;
length++;
}
tail->next = head;
k = k % length;
// rotating k times is the same as rotating k%length times
Node *newTail = head;
for (int i = 0; i < length - k - 1; i++)
{
newTail = newTail->next;
}
newTail->next = nullptr;
}
// *********
// rotating k times is the same as rotating k%length times
// *******
// Grid Path problem
struct GridNode
{
int x, y; // Coordinates of the cell
GridNode *next; // Pointer to the next cell in the path
GridNode(int xCoord, int yCoord) : x(xCoord), y(yCoord), next(nullptr) {}
};
// Function to add a new cell to the path
void addCell(GridNode *&head, int x, int y)
{
GridNode *newGridNode = new GridNode(x, y);
if (head == nullptr)
{
head = newGridNode; // If the path is empty, this is the first cell
}
else
{
// Traverse to the end of the list and add the new cell
GridNode *current = head;
while (current->next != nullptr)
{
current = current->next;
}
current->next = newGridNode;
}
}
// Recursive functions
// 1.print a Linked List (Recursive)
void printReverse(Node *head)
{
if (!head)
return;
printReverse(head->next);
cout << head->data << " ";
}
// 2. Reverse a LinkedList (Recursive)
Node *ReverseList(Node *head)
{
if (!head || !head->next)
return head;
Node *newHead = ReverseList(head->next);
head->next->next = head;
head->next = nullptr;
return newHead;
}
// 3. Merge Sorted LinkedList
Node *mergeTwoLists(Node *l1, Node *l2)
{
if (!l1)
return l2;
if (!l2)
return l1;
if (l1->data < l2->data)
{
l1->next = mergeTwoLists(l1->next, l2);
return l1;
}
else
{
l2->next = mergeTwoLists(l1, l2->next);
return l2;
}
}
// 4. Palindrome
bool Palindrome(Node *left, Node *right)
{
if (right == nullptr)
return true;
bool isPal = Palindrome(left, right->next);
if (!isPal)
return false;
bool isEqual = left->data == right->data;
left = left->next;
return isEqual;
}
// Reverse LinkedList recursively and iteratively
Node *ReverseRecursive(Node *head)
{
if (head->next == nullptr)
return head;
Node *newTail = ReverseRecursive(head);
head->next->next = head;
head->next = nullptr;
return newTail;
}
Node *Reverse(Node *head)
{
if (head == nullptr)
return head;
Node *curr = head;
Node *prev = nullptr;
Node *next = nullptr;
while (curr != nullptr)
{
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
head = prev;
return head;
}