-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.cpp
More file actions
925 lines (829 loc) · 21 KB
/
Copy pathLinkedList.cpp
File metadata and controls
925 lines (829 loc) · 21 KB
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
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
// Why LL?
// 1. Dynamic Size - unlike arrays they're not contiguous
// 2. Easy to insert and delete
// 3. Random Access is not possible
// 4. Memory is allocated at runtime
// 5. No memory wastage
// 6. No memory shortage
// 7. No need to give size at the time of declaration
// 8. No need to give the type of data at the time of declaration
// Starting point of the Linked is called Head of the Linked
// And the head of the Linked is stored at the m1 location
// Ending point of the Linked is called Tail of the Linked
// Used in stack, queue mostly
// In real life it's used in a browser
// Node x=Node(2,nullptr);
// Node* y=&x;
// OR
// Node* y=new Node(2,nullptr); // It automatically stores a pointer to the memory location
// int takes 4 bytes
// pointer takes 4 bytes in 32 bit system and 8 bytes in 64 bit system
#include <bits/stdc++.h>
using namespace std;
// use class instead of struct because we want to use OOPs concepts
class Node
{
public:
int data;
Node *next;
public:
// Constructor - It's a special type of function that is called when an object is created
// It's used to initialize the object
Node(int data1)
{
data = data1;
next = nullptr;
}
};
// What is a node? - A node is a block of memory that stores data and a pointer to the next node
// What is a Linked? - A Linked is a collection of nodes
// What is a singly Linked? - A Linked in which each node has a pointer to the next node
Node *convertArr2LL(vector<int> &arr)
{
Node *head = new Node(arr[0]); // head is a pointer that points to the first node
Node *mover = head; // mover is a pointer that points to the head and the first node points to null as of now
for (int i = 1; i < arr.size(); i++)
{
Node *temp = new Node(arr[i]); // temp is a pointer that points to the current node
mover->next = temp; // the node which mover points to is made to point to the temp node
// mover=mover->next; // can use this too same as below
mover = temp; // mover is made to point to the temp node
}
return head;
}
void printLL(Node *head)
{
Node *temp = head;
while (temp)
{
cout << temp->data << " ";
temp = temp->next;
}
cout << "\n";
}
int lengthOfLL(Node *head)
{
int count = 0;
Node *temp = head;
while (temp)
{
count++;
temp = temp->next;
}
return count;
}
int checkIfPresent(Node *head, int k)
{
Node *temp = head;
while (temp)
{
if (temp->data == k)
{
return temp->data;
break;
}
temp = temp->next;
}
return -1;
}
Node *removeHead(Node *head)
{
if (head == NULL)
return nullptr;
Node *temp = head;
head = head->next;
free(temp); // or delete temp;
return head;
}
Node *removeTail(Node *head)
{
if (head == NULL || head->next == NULL)
return NULL; // But here the one element is not deleted
Node *temp = head;
while (temp->next->next != nullptr)
{
temp = temp->next;
}
free(temp->next); // But after this shouldn't temp should point to null automatically? No
temp->next = nullptr;
return head;
}
Node *removeKthNode(Node *head, int k)
{
if (head == NULL)
return head;
if (k == 1)
return removeHead(head);
Node *temp = head;
int i = 0;
while (temp != NULL)
{
if (i == k - 2)
{
Node *temp2 = temp->next;
temp->next = temp->next->next;
free(temp2);
break;
}
i++;
temp = temp->next;
}
return head;
}
Node *removeEL(Node *head, int el)
{
if (head == NULL)
return head;
if (head->data == el)
return removeHead(head);
Node *temp = head;
Node *prev = NULL;
while (temp != nullptr)
{
if (temp->data == el)
{
prev->next = temp->next;
free(temp);
break;
}
prev = temp;
temp = temp->next;
}
return head;
}
Node *insertHead(Node *head, int el)
{
Node *newHead = new Node(el);
newHead->next = head;
return newHead;
}
Node *insertTail(Node *head, int el)
{
if (head == NULL)
return new Node(el);
Node *temp = head;
while (temp->next != NULL)
{
temp = temp->next;
}
Node *newTail = new Node(el);
temp->next = newTail;
return head;
}
Node *insertKthNode(Node *head, int k, int el)
{
if (head == NULL)
return new Node(el);
if (k == 1)
return insertHead(head, el);
Node *temp = head;
int i = 0;
while (temp != NULL)
{
if (i == k - 2)
{
Node *temp2 = new Node(el);
temp2->next = temp->next;
temp->next = temp2;
break;
}
i++;
temp = temp->next;
}
return head;
}
Node *insertBeforeX(Node *head, int x, int el)
{
if (head == NULL)
return new Node(el);
if (head->data == x)
return insertHead(head, el);
Node *temp = head;
Node *prev = NULL;
while (temp != NULL)
{
if (temp->data == x)
{
Node *temp2 = new Node(el);
temp2->next = temp;
prev->next = temp2;
break;
}
prev = temp;
temp = temp->next;
}
return head;
}
Node *middleOfLL(Node *head)
{
// Brute
// Time complexity - O(n) with two passes
// Space complexity - O(1)
// if (head == nullptr || head->next == nullptr)
// return head;
// Node *temp = head;
// int count = 0;
// while (temp)
// {
// count++;
// temp = temp->next;
// }
// int mid = count / 2 + 1;
// temp = head, count = 0;
// while (temp)
// {
// count++;
// if (count == mid)
// break;
// temp = temp->next;
// }
// return temp;
// Optimized
// Tortoise and Hare Algorithm
// Time complexity - O(n) with one pass
// Space complexity - O(1)
if (head == nullptr || head->next == nullptr)
return head;
Node *slow = head;
Node *fast = head;
while (fast != nullptr && fast->next != nullptr)
{
slow = slow->next;
fast = fast->next->next;
}
return slow;
}
Node *reverseLL(Node *head)
{
// Brute
// Using stack same as we did in DLL
// Time complexity - O(n) with two pass
// Space complexity - O(n) with extra space for stack
// Optimal
// Iterative approach
// Using 3 pointers - prev, temp, front
// Time complexity - O(n) with one pass
// Space complexity - O(1)
// if (head == nullptr || head->next == nullptr)
// return head;
// Node *prev = nullptr;
// Node *temp = head;
// while (temp != nullptr)
// {
// Node *front = temp->next;
// temp->next = prev;
// prev = temp;
// temp = front;
// }
// return prev;
// Recursive approach
// Time complexity - O(n) with one pass
// Space complexity - O(n) with extra space for recursion stack
if (head == nullptr || head->next == nullptr)
return head;
Node *newHead = reverseLL(head->next);
Node *front = head->next;
front->next = head;
head->next = nullptr;
return newHead;
}
bool hasLoop(Node *head)
{
// Brute
// Time complexity - O(n)
// Space complexity - O(n) with extra space for hash map
// unordered_map<Node *, int> mpp;
// Node *temp = head;
// while (temp)
// {
// if (mpp.find(temp) != mpp.end())
// mpp[temp] = 1;
// else
// return true;
// temp = temp->next;
// }
// return false;
// Optimal
// Tortoise and Hare Algorithm
// If there is a loop, then the fast pointer will surely meet the slow pointer coz in every iteration the distance between them will be reduced by 1
// Time complexity - O(n) with one pass
// Space complexity - O(1)
Node *slow = head;
Node *fast = head;
while (fast != nullptr && fast->next != nullptr)
{
slow = slow->next;
fast = fast->next->next;
if (slow == fast)
return true;
}
return false;
}
Node *startOfLoop(Node *head)
{
// Brute
// Time complexity - O(n) with two passes
// Space complexity - O(n)
// same as hasLoop function
// Optimal
// Tortoise and Hare Algorithm
// Time complexity - O(n) with one pass
// Space complexity - O(1)
Node *slow = head;
Node *fast = head;
while (fast != nullptr && fast->next != nullptr)
{
slow = slow->next;
fast = fast->next->next;
if (slow == fast)
break;
}
if (fast == nullptr || fast->next == nullptr)
return nullptr; // No loop
slow = head;
// the distance between the slow and fast pointer will be equal because??
while (slow != fast)
{
slow = slow->next;
fast = fast->next;
}
return slow; // or fast
}
int lengthOfLoop(Node *head)
{
// Brute
// Time complexity - O(n) with two passes
// Space complexity - O(n)
// unordered_map<Node *, int> mpp;
// Node *temp = head;
// int count = 0;
// while (temp)
// {
// if (mpp.find(temp) != mpp.end())
// {
// count = count - mpp[temp];
// return count;
// }
// mpp[temp] = count;
// count++;
// temp = temp->next;
// }
// return 0;
// Optimal
// Tortoise and Hare Algorithm
// Time complexity - O(n) with one pass
// Space complexity - O(1)
Node *slow = head;
Node *fast = head;
while (fast != nullptr && fast->next != nullptr)
{
slow = slow->next;
fast = fast->next->next;
if (slow == fast)
{
int count = 1;
slow = slow->next;
while (slow != fast)
{
slow = slow->next;
count++;
}
return count;
}
}
return 0; // No loop
}
bool checkPanlindrome(Node *head)
{
// Brute
// Using stack
// just push the elements in the stack and then pop them and check if they are equal or not
// Time complexity - O(n) with two passes
// Space complexity - O(n) with extra space for stack
// Optimal
// Time complexity - O(n) with two pass
// Space complexity - O(1)
if (head == nullptr || head->next == nullptr)
return true;
// Middle Of LL
Node *slow = head;
Node *fast = head;
while (fast->next != nullptr && fast->next->next != nullptr)
{
slow = slow->next;
fast = fast->next->next;
}
Node *newHead = reverseLL(slow->next);
// Check if 1st & 2nd half are equal
Node *first = head;
Node *second = newHead;
while (second)
{
if (first->data != second->data)
{
reverseLL(newHead);
return false;
}
first = first->next;
second = second->next;
}
reverseLL(newHead);
return true;
}
Node *oddEven(Node *head)
{
// Brute
// Using array just store even and odd and then overwrite the LL
// Time complexity - O(n) with two passes
// Space complexity - O(n) with extra space for array
// Optimal
// Time complexity - O(n) with one pass
// Space complexity - O(1)
if (head == nullptr || head->next == nullptr || head->next->next == nullptr)
return head;
Node *odd = head;
Node *even = head->next;
Node *temp = even;
while (even != nullptr && even->next != nullptr)
{
odd->next = odd->next->next;
even->next = even->next->next;
odd = odd->next;
even = even->next;
}
odd->next = temp;
return head;
}
Node *removeNthFromEnd(Node *head, int n)
{
// Brute
// Time complexity - O(n) with two passes
// Space complexity - O(1)
// if (head->next == nullptr && n == 1)
// return nullptr;
// Node *temp = head;
// int len = 0;
// while (temp)
// {
// len++;
// temp = temp->next;
// }
// if (len == n)
// {
// Node *temp = head;
// Node *newHead = head->next;
// delete temp;
// return newHead;
// }
// temp = head;
// int pos = len - n;
// while (temp)
// {
// pos--;
// if (pos == 0)
// {
// Node *delNode = temp->next;
// if (n == 1)
// temp->next = nullptr;
// else
// temp->next = temp->next->next;
// delete delNode;
// break;
// }
// temp = temp->next;
// }
// return head;
// Optimal
// Time complexity - O(n) with one pass
// Space complexity - O(1)
if (head->next == nullptr && n == 1)
return nullptr;
Node *fast = head;
int count = 1;
while (count != n)
{
fast = fast->next;
count++;
}
if (fast->next == nullptr)
{
Node *newHead = head->next;
delete head;
return newHead;
}
Node *slow = head;
while (fast->next->next != nullptr)
{
slow = slow->next;
fast = fast->next;
}
Node *temp = slow->next;
if (n == 1)
slow->next = nullptr;
else
slow->next = slow->next->next;
delete temp;
return head;
}
Node *deleteMiddle(Node *head)
{
// Brute
// Find the length of the LL and then find the middle element and delete it
// Time complexity - O(n) with two passes
// Space complexity - O(1)
// Optimal
// Time complexity - O(n) with one pass
// Space complexity - O(1)
if (head->next == nullptr)
return nullptr;
Node *fast = head;
Node *slow = head;
while (fast->next->next != nullptr && fast->next->next->next != nullptr)
{
slow = slow->next;
fast = fast->next->next;
}
Node *temp = slow->next;
if (slow->next->next == nullptr)
slow->next = nullptr;
else
slow->next = slow->next->next;
delete temp;
return head;
}
Node *midOfLL(Node *head)
{
Node *slow = head;
Node *fast = head;
while (fast->next != nullptr && fast->next->next != nullptr)
{
slow = slow->next;
fast = fast->next->next;
}
return slow;
}
Node *merge(Node *leftHead, Node *rightHead)
{
Node *dummyNode = new Node(-1);
Node *temp = dummyNode;
while (leftHead != nullptr && rightHead != nullptr)
{
if (leftHead->data <= rightHead->data)
{
temp->next = leftHead;
temp = temp->next;
leftHead = leftHead->next;
}
else
{
temp->next = rightHead;
temp = temp->next;
rightHead = rightHead->next;
}
}
if (leftHead)
temp->next = leftHead;
else
temp->next = rightHead;
return dummyNode->next;
}
Node *ms(Node *head)
{
if (head == nullptr || head->next == nullptr)
return head;
Node *mid = midOfLL(head);
Node *leftHead = head;
Node *rightHead = mid->next;
mid->next = nullptr;
leftHead = ms(leftHead);
rightHead = ms(rightHead);
return merge(leftHead, rightHead);
}
Node *sortLL(Node *head)
{
// Brute
// Using array just store the elements in the array and then sort the array and then overwrite the LL
// Time complexity - O(nlogn) with two passes
// Space complexity - O(n) with extra space for array
// Optimal
// Time complexity - O(nlogn)
// Space complexity - O(1) for extra space but O(log n) for recursion stack
return ms(head);
}
Node *sortLLwith012(Node *head)
{
// Brute
// take three counters for 0, 1 and 2 and then overwrite the LL
// Time complexity - O(n) with two passes
// Space complexity - O(1)
// Optimal
// Time complexity - O(n) with one pass
// Space complexity - O(1)
if (head == nullptr || head->next == nullptr)
return head;
Node *zero = new Node(-1);
Node *temp0 = zero;
Node *one = new Node(-1);
Node *temp1 = one;
Node *two = new Node(-1);
Node *temp2 = two;
Node *temp = head;
while (temp)
{
if (temp->data == 0)
{
zero->next = temp;
zero = zero->next;
}
else if (temp->data == 1)
{
one->next = temp;
one = one->next;
}
else
{
two->next = temp;
two = two->next;
}
temp = temp->next;
}
zero->next = (temp1->next != nullptr) ? temp1->next : temp2->next;
one->next = temp2->next;
two->next = nullptr;
delete zero;
delete one;
delete two;
return temp0->next;
}
Node *poi(Node *smaller, Node *grater, int d)
{
Node *temp1 = smaller;
Node *temp2 = grater;
while (d > 0)
{
temp2 = temp2->next;
d--;
}
while (temp2)
{
if (temp1 == temp2)
return temp1;
temp1 = temp1->next;
temp2 = temp2->next;
}
return nullptr;
}
Node *getIntersectionNode(Node *headA, Node *headB)
{
// Brute
// Using hash map just store the elements of one LL in the hash map and then check if the other LL has any of those elements
// Time complexity - O(n)
// Space complexity - O(n) the extra space for hash map
// Better
// Time complexity - O(n1+n2) with two passes
// Space complexity - O(1)
// Node *tempA = headA;
// int countA = 0;
// Node *tempB = headB;
// int countB = 0;
// while (tempA)
// {
// countA++;
// tempA = tempA->next;
// }
// while (tempB)
// {
// countB++;
// tempB = tempB->next;
// }
// if (countA <= countB)
// return poi(headA, headB, countB - countA);
// else
// return poi(headB, headA, countA - countB);
// Optimal
// suppose the samller LL is x steps ahead...then when it reaches null, then it comes to the larger LL and covers the x steps and until then the larger LL has also covered x steps and now they are at the same distance from the intersection point
// Time complexity - O(n1+n2) with one pass
// Space complexity - O(1)
Node *temp1 = headA;
Node *temp2 = headB;
while (temp1 != temp2)
{
temp1 = temp1->next;
temp2 = temp2->next;
if (temp1 == temp2)
return temp1;
if (temp1 == nullptr)
temp1 = headB;
else if (temp2 == nullptr)
temp2 = headA;
}
return temp1;
}
int carryHelper(Node *head)
{
if (head == nullptr)
return 1;
int carry = carryHelper(head->next);
head->data += carry;
if (head->data < 10)
return 0;
head->data = 0;
return 1;
}
Node *addOne(Node *head)
{
// Better
// Time complexity - O(n) with three passes
// Space complexity - O(1)
// Node *revHead = reverseLL(head);
// Node *temp = revHead;
// int carry = 1;
// while (temp)
// {
// if (temp->data + carry == 10)
// {
// temp->data = 0;
// carry = 1;
// }
// else
// {
// temp->data += carry;
// carry = 0;
// break;
// }
// temp = temp->next;
// }
// if(carry == 1)
// {
// Node *newNode = new Node(1);
// newNode->next = revHead;
// return newNode;
// }
// return reverseLL(revHead);
// Optimal
// Time complexity - O(n) with one pass
// Space complexity - O(n) for recursion stack
int carry = carryHelper(head);
if (carry == 1)
{
Node *newHead = new Node(1);
newHead->next = head;
return newHead;
}
return head;
}
Node *addTwoNumbers(Node *l1, Node *l2)
{
// Optimal
// Time complexity - O(max(n1, n2)) with one pass
// Space complexity - O(1) for extra space but O(n) for returning the ans
Node *sumNode = new Node(-1);
Node *head = sumNode;
int carry = 0;
while (l1 != nullptr || l2 != nullptr)
{
int sum = carry;
if (l1)
sum += l1->data;
if (l2)
sum += l2->data;
Node *newNode = new Node(sum % 10);
carry = sum / 10;
sumNode->next = newNode;
sumNode = sumNode->next;
if (l1)
l1 = l1->next;
if (l2)
l2 = l2->next;
}
if (carry)
{
Node *newNode = new Node(carry);
sumNode->next = newNode;
sumNode = sumNode->next;
}
return head->next;
}
int main()
{
vector<int> arr = {4, 5, 6};
// Node y= Node(arr[0],nullptr); // Simply an object
// cout<<y.data<<"\n";
// cout<<y.next<<"\n";
// Node* y=new Node(arr[0],nullptr); // Pointer to the memory location
// cout<<y<<"\n";
Node *head = convertArr2LL(arr);
// cout<<head->data<<"\n";
// Node* temp=head;
// while(temp)
// {
// cout<<temp->data<<" ";
// temp=temp->next;
// }
// cout<<lengthOfLL(head)<<"\n";
// cout<<checkIfPresent(head,8)<<"\n";
printLL(head);
head = addOne(head);
printLL(head);
// cout << addOne(head) << "\n";
}