-
Notifications
You must be signed in to change notification settings - Fork 22
/
LinkedListUtil.java
1391 lines (1182 loc) · 41.8 KB
/
LinkedListUtil.java
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
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package com.geeksforgeeks.linkedlist;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
public class LinkedListUtil {
public Node head;
public class Node {
int data;
Node next;
int size;
Node down; // ===> For Flattening Purpose
public Node(int data) {
this.data = data;
}
public Node() {
}
}
public void insert(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
return;
}
Node temp = head;
newNode.next = head;
head = newNode;
}
public void appendDown(Node nodeOnWhichDataToBeAppended, int data) {
Node newNode = new Node(data);
Node temp = nodeOnWhichDataToBeAppended;
if (head == null) {
head = nodeOnWhichDataToBeAppended;
return;
}
while (temp.down != null) {
temp = temp.down;
}
temp.down = newNode;
}
public void append(int data) {
Node newNode = new Node(data);
Node temp = head;
if (head == null) {
head = newNode;
return;
}
while (temp.next != null) {
temp = temp.next;
}
temp.next = newNode;
}
public void append(Node newNode) {
Node temp = head;
if (head == null) {
head = newNode;
return;
}
while (temp.next != null) {
temp = temp.next;
}
temp.next = newNode;
}
public int getSize(Node head) {
if (head == null) {
return 0;
}
return 1 + getSize(head.next);
}
public void deleteKey(int dataToBeDeleted) {
Node temp = head;
Node prev = null;
while (temp != null) {
if (temp.data == dataToBeDeleted) {
if (prev == null) { // List with only 1 node and we are deleting that node
if (temp.next != null) {
head = temp.next;
} else {
head = null;
}
break;
} else {
prev.next = temp.next;
}
}
prev = temp;
temp = temp.next;
}
printList(head);
}
public static Node getMiddleElementOfList(Node head) {
Node slowPointer = head;
Node fastPointer = head;
if (head == null) {
return head;
}
while (fastPointer != null) {
fastPointer = fastPointer.next;
if (fastPointer != null && fastPointer.next != null) {
slowPointer = slowPointer.next;
fastPointer = fastPointer.next.next;
}
}
System.out.println("===========================Finding the Middle element of list : ");
printList(head);
System.out.println("The Middle Element of the list is " + slowPointer.data);
return slowPointer;
}
public static void printList(Node head) {
List<Integer> items = new ArrayList<>();
while (head != null && head != null) {
items.add(head.data);
head = head.next;
}
System.out.println(items);
}
public void printSize(Node head) {
System.out.println("Size of List is " + getSize(head));
}
public void swapNodesWithoutSwappingData(int A, int B) {
Node prevA = null;
Node nodeA = head;
Node prevB = null;
Node nodeB = head;
Boolean nodeAPresent = false;
Boolean nodeBPresent = false;
while (nodeA != null && nodeB != null && (!nodeAPresent || !nodeBPresent)) {
if (!nodeAPresent) {
if (nodeA.data == A) {
nodeAPresent = true;
} else {
prevA = nodeA;
nodeA = nodeA.next;
}
}
if (!nodeBPresent) {
if (nodeB.data == B) {
nodeBPresent = true;
} else {
prevB = nodeB;
nodeB = nodeB.next;
}
}
}
if (nodeAPresent && nodeBPresent) {
if (prevA == null) {
head = nodeB;
} else {
prevA.next = nodeB;
}
if (prevB == null) {
head = nodeA;
} else {
prevB.next = nodeA;
}
Node temp = nodeA.next;
nodeA.next = nodeB.next;
nodeB.next = temp;
System.out.println("Swap done and after swapping " + A + "," + B + " from the list is :");
printList(head);
} else {
System.out.println("=====================Node(s) doesn't exist in the list=========================");
}
}
public static void printKthElementFromLast(Node head, int k) {
Node lazyPointer = head;
Node fastPointer = head;
int count = 0;
while (fastPointer != null) {
while (count++ < k) {
fastPointer = fastPointer.next;
if (fastPointer == null) {
System.out.println("Invalid value of " + k);
break;
}
}
if (fastPointer != null) {
lazyPointer = lazyPointer.next;
fastPointer = fastPointer.next;
}
}
System.out.println("=================Finding the kth :: " + k + " Element from Last================");
printList(head);
System.out.println(lazyPointer.data);
}
public static void reverseList(Node head) {
Node prev = null;
Node temp = head;
Node next;
while (temp != null) {
next = temp.next;
temp.next = prev;
prev = temp;
temp = next;
}
printList(prev);
}
public static Node reverseListRecursively(Node head, Node prev) {
if (head == null) {
return prev;
}
Node next = head.next;
head.next = prev;
return reverseListRecursively(next, head);
}
public static Boolean listHasLoop(Node head) {
Boolean hasLoop = false;
Node slowPointer = head;
Node fastPointer = head;
while (fastPointer != null && fastPointer.next != null) {
fastPointer = fastPointer.next.next;
slowPointer = slowPointer.next;
if (fastPointer == slowPointer) {
hasLoop = true;
break;
}
}
return hasLoop;
}
/**
* SortedMerge() function that takes two lists, each of which is sorted in increasing order,
* and merges the two together into one list which is in increasing order.
* <p>
* 5->10->15 + 2->3->20 = 2->3->5->10->15->20.
*
* @param headA
* @param headB
*/
public Node sortedMerge(Node headA, Node headB) {
Node tempA = headA;
Node tempB = headB;
LinkedListUtil util = new LinkedListUtil();
while (tempA != null && tempB != null) {
if (tempA.data < tempB.data) {
util.append(new Node(tempA.data));
tempA = tempA.next;
} else {
util.append(new Node(tempB.data));
tempB = tempB.next;
}
}
while (tempA != null) {
util.append(new Node(tempA.data));
tempA = tempA.next;
}
while (tempB != null) {
util.append(new Node(tempB.data));
tempB = tempB.next;
}
return util.head;
}
/**
* Given a singly linked list of characters, write a function that returns true if the given list is palindrome, else false.
* <p>
* R -> A -> D -> A -> R
* <p>
* M -> A -> D -> D -> A -> M
*
* @param head
* @return
*/
public Boolean isPalindromicList(Node head) {
Boolean isPalindromicList = false;
int size = getSize(head);
Node firstHalf = head;
firstHalf.size = size / 2;
Node middleElement = getMiddleElementOfList(head);
// Here taking care of odd and even list, if Odd length list then we should reverse the list after the middle element else start from the middle element itself
Node secondHalf = reverseListRecursively(size % 2 == 0 ? middleElement : middleElement.next, null);
if (compareLists(firstHalf, secondHalf)) {
isPalindromicList = true;
}
return isPalindromicList;
}
public Boolean compareLists(Node headA, Node headB) {
Boolean isEqual = true;
int counterA = 0;
int sizeA = headA.size;
while (headA != null && counterA < sizeA && headB != null) {
if (headA.data == headB.data) {
headA = headA.next;
headB = headB.next;
counterA++;
continue;
}
isEqual = false;
break;
}
if (headA != null && headB != null) {
isEqual = false;
}
return isEqual;
}
public static Boolean isPalindromicListUsingStack(Node head) {
Boolean isPalindromicList = true;
Node slowPointer = head;
Node fastPointer = head;
Stack<Integer> stack = new Stack<>();
Boolean isEvenList = false;
while (fastPointer != null && fastPointer.next != null) {
stack.push(slowPointer.data);
slowPointer = slowPointer.next;
fastPointer = fastPointer.next.next;
}
if (fastPointer != null && fastPointer.next == null) { // i.e Odd length List we compare from Next of middle item
slowPointer = slowPointer.next;
}
while (!stack.isEmpty()) {
if (slowPointer.data == stack.peek()) {
slowPointer = slowPointer.next;
stack.pop();
continue;
}
isPalindromicList = false;
break;
}
return isPalindromicList;
}
/**
* 11->11->11->21->43->43->60
* <p>
* 11->21->43->60
*
* @param head
*/
public void removeDuplicateFromSortedList(Node head) {
Node current = head;
Node nextOfCurrentNext;
if (current == null) {
System.out.println("List is Empty");
return;
}
while (current.next != null) {
if (current.data == current.next.data) {
nextOfCurrentNext = current.next.next;
current.next = null;
current.next = nextOfCurrentNext;
} else {
current = current.next;
}
}
printList(head);
}
/**
* 12->11->12->21->41->43->21
* <p>
* 12->11->21->41->43
*
* @param head
*/
public void removeDuplicateFromUnsortedList(Node head) {
if (head == null) {
System.out.println("List is Empty");
return;
}
Node current = head;
Node nextOfCurrentsNext;
List<Integer> visitedNodes = new ArrayList<>();
visitedNodes.add(head.data);
while (current.next != null) {
if (visitedNodes.contains(current.next.data)) {
visitedNodes.add(current.data);
nextOfCurrentsNext = current.next.next;
current.next = null;
current.next = nextOfCurrentsNext;
} else {
current = current.next;
visitedNodes.add(current.data);
}
}
printList(head);
}
/**
* 1->2->3->4->5 ===> 2->1->4->3->5,
* <p>
* <p>
* 1->2->3->4->5->6 ==> 2->1->4->3->6->5.
*
* @param head
*/
public void reverseListInGroup(Node head, int groupSize) {
if (head == null) {
System.out.println("List is empty");
return;
}
Node current = head;
int count;
Node prev;
Node next;
Node thisHead;
Node prevHead = null;
while (current != null) {
count = 0;
thisHead = current;
prev = null;
while (count++ < groupSize) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
if (prevHead != null) {
prevHead.next = prev;
} else {
head = prev; // Since Initial swap the first element becomes last element of the first chunk
}
prevHead = thisHead;
}
printList(head);
}
/**
* Delete alternate nodes of a Linked List
* Given a Singly Linked List, starting from the second node delete all alternate nodes of it.
* For example, if the given linked list is 1->2->3->4->5 then your function should convert it to 1->3->5,
* and if the given linked list is 1->2->3->4 then convert it to 1->3.
*
* @param head
*/
public void deleteAlternativeNode(Node head) {
System.out.println("Let's delete Alternative Nodes of below list");
printList(head);
Node temp = head;
Node next = null;
while (temp != null && temp.next != null) {
next = temp.next.next;
temp.next = next;
temp = next;
}
printList(head);
}
/**
* Reverse alternate K nodes in a Singly Linked List ------> Difficulty 3.3
* Given a linked list, write a function to reverse every alternate k nodes (where k is an input to the function) in an efficient way. Give the complexity of your algorithm.
* <p>
* Example:
* Inputs: 1->2->3->4->5->6->7->8->9->NULL and k = 3
* Output: 3->2->1->4->5->6->9->8->7->NULL.
*
* @param head
* @param k
*/
public void reverseAlternateKNodes(Node head, int k) {
Node current = head;
Node next;
Node prev = null;
Node thisHead;
Node prevHead = null;
int counter;
Boolean shouldReverse = true;
while (current != null) {
thisHead = current;
counter = 0;
prev = null;
if (shouldReverse) {
while (current != null && counter++ < k) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
shouldReverse = false;
if (prevHead == null) {
head = prev;
} else {
prevHead.next = prev;
}
prevHead = thisHead;
} else {
prevHead.next = current;
while (current != null && counter++ < k) {
prev = current;
current = current.next;
}
shouldReverse = true;
prevHead = prev;
}
}
printList(head);
}
/**
* 1) Reverse the list
* 2) Identify Nodes to be deleted
* 3) Reverse the list again to get the original answer
*
* @param head
*/
public static void deleteNodesHavingGreaterValueAtRightSide(Node head) {
Node reversedList = reverseListRecursively(head, null);
int maxTillNow = 0;
Node temp = reversedList;
Node prev = null;
Node next = null;
while (temp != null) {
if (temp.data > maxTillNow) {
maxTillNow = temp.data;
prev = temp;
temp = temp.next;
} else {
next = temp.next;
prev.next = next;
temp = next;
}
}
printList(reverseListRecursively(reversedList, null));
}
/**
* Input: 17->15->8->12->10->5->4->1->7->6->NULL
* Output: 8->12->10->4->6->17->15->5->1->7->NULL
* <p>
* Input: 8->12->10->5->4->1->6->NULL
* Output: 8->12->10->4->6->5->1->NULL
* <p>
* // If all numbers are even then do not change the list
* Input: 8->12->10->NULL
* Output: 8->12->10->NULL
* <p>
* // If all numbers are odd then do not change the list
* Input: 1->3->5->7->NULL
* Output: 1->3->5->7->NULL
*
* @param head
*/
public static void segregateEvenAndOddNumbers(Node head) {
Node current = head;
Node evenHead = null, oddHead = null, lastEvenNode = null, lastOddNode = null;
while (current != null) {
if (current.data % 2 == 0) {
if (evenHead == null) {
evenHead = lastEvenNode = current;
} else {
lastEvenNode.next = current;
lastEvenNode = current;
}
} else {
if (oddHead == null) {
oddHead = lastOddNode = current;
} else {
lastOddNode.next = current;
lastOddNode = current;
}
}
current = current.next;
}
if (evenHead != null) {
head = evenHead;
if (oddHead != null) {
lastEvenNode.next = oddHead;
lastOddNode.next = null;
}
} else {
head = oddHead;
}
printList(head);
}
/**
* Using Extended Floydd Algorithm
*
* @param head
*/
public void detectAndRemoveLoop(Node head) {
Node slowPointer = head;
Node fastPointer = head;
slowPointer = slowPointer.next;
fastPointer = fastPointer.next.next;
while (fastPointer != null && fastPointer.next != null) {
if (slowPointer == fastPointer) {
break;
}
slowPointer = slowPointer.next;
fastPointer = fastPointer.next.next;
}
// Loop Exist
if (slowPointer == fastPointer) {
System.out.println("Yes Loop Exist");
slowPointer = head;
while (slowPointer.next != fastPointer.next) {
slowPointer = slowPointer.next;
fastPointer = fastPointer.next;
}
fastPointer.next = null;
} else {
System.out.println("No Loop Exist");
}
printList(head);
}
/**
* Input:
* First List: 5->6->3 // represents number 365
* Second List: 8->4->2 // represents number 248
* Output
* Resultant list: 3->1->6 // represents number 613
* <p>
* Input:
* First List: 7->5->9->4->6 // represents number 64957
* Second List: 8->4 // represents number 48
* Output
* Resultant list: 5->0->0->5->6 // represents number 65005
* <p>
* Problem Number : 41 Linked List
*
* @param head
*/
public static void addNumbersRepresentedByList(Node first, Node second) {
Node firstTemp = first;
Node secondTemp = second;
int result = 0, digitFromFirst = 0, digitFromSecond = 0, carry = 0;
LinkedListUtil resultList = new LinkedListUtil();
while (firstTemp != null && secondTemp != null) {
digitFromFirst = firstTemp.data;
digitFromSecond = secondTemp.data;
result = digitFromFirst + digitFromSecond;
if (carry == 1) {
result += 1;
}
carry = result / 10;
resultList.append(result >= 10 ? result % 10 : result);
firstTemp = firstTemp.next;
secondTemp = secondTemp.next;
}
while (firstTemp != null) {
digitFromFirst = firstTemp.data;
result = carry == 1 ? digitFromFirst + 1 : digitFromFirst;
carry = result / 10;
resultList.append(result >= 10 ? result % 10 : result);
firstTemp = firstTemp.next;
}
while (secondTemp != null) {
digitFromSecond = secondTemp.data;
result = carry == 1 ? digitFromSecond + 1 : digitFromSecond;
carry = result / 10;
resultList.append(result >= 10 ? result % 10 : result);
secondTemp = secondTemp.next;
}
printList(resultList.head);
}
/**
* For example, if the three linked lists are 12->6->29, 23->5->8 and 90->20->59,
* and the given number is 101, the output should be tripel “6 5 90”.
* <p>
* Problem Number : 42 Linked List
*/
public void findTriplet(Node headA, Node headB, Node headC, int sumToBeTested) {
Node A = headA;
Node B = headB;
Node C = headC;
B = mergeSort(B, false);
C = mergeSort(C, true);
Node tempB = B;
Node tempC = C;
while (A != null) {
tempB = B;
tempC = C;
while (tempB != null && tempC != null) {
int result = A.data + tempB.data + tempC.data;
if (result == sumToBeTested) {
System.out.println("Triplet found " + A.data + "+" + tempB.data + "+" + tempC.data);
return;
}
if (result < sumToBeTested) {
tempB = tempB.next;
} else {
tempC = tempC.next;
}
}
A = A.next;
}
}
public Node mergeSort(Node head, Boolean isReverseSort) {
//If No Nodes are present or only 1 node is present return the same as single node or no node is already sorted
if (head == null || head.next == null) {
return head;
}
Node middle = getMiddleElementOfList(head);
Node nextOfMiddle = middle.next;
middle.next = null;
Node left = mergeSort(head, isReverseSort);
Node right = mergeSort(nextOfMiddle, isReverseSort);
LinkedListUtil sortedList = new LinkedListUtil();
sortedMerge(left, right, sortedList, isReverseSort);
return sortedList.head;
}
private void sortedMerge(Node headOfFirstPartOfList, Node headofSecondPartOfList, LinkedListUtil sortedList, Boolean isReverseSort) {
Node t1 = headOfFirstPartOfList;
Node t2 = headofSecondPartOfList;
if (isReverseSort) {
while (t1 != null && t2 != null) {
if (t1.data > t2.data) {
sortedList.append(t1.data);
t1 = t1.next;
} else {
sortedList.append(t2.data);
t2 = t2.next;
}
}
while (t2 != null) {
sortedList.append(t2.data);
t2 = t2.next;
}
while (t1 != null) {
sortedList.append(t1.data);
t1 = t1.next;
}
} else {
while (t1 != null && t2 != null) {
if (t1.data < t2.data) {
sortedList.append(t1.data);
t1 = t1.next;
} else {
sortedList.append(t2.data);
t2 = t2.next;
}
}
while (t1 != null) {
sortedList.append(t1.data);
t1 = t1.next;
}
while (t2 != null) {
sortedList.append(t2.data);
t2 = t2.next;
}
}
}
/**
* Left Rotate it is
* For example, if the given linked list is 10->20->30->40->50->60 and k is 4,
* the list should be modified to 50->60->10->20->30->40.
*/
public static void rotateList(Node head, int k) {
int count = 1;
Node temp = head;
Node next;
Node toBeFirstNode;
while (++count <= k) {
temp = temp.next;
if (temp == null) {
temp = head;
}
}
next = temp.next;
temp.next = null;
if (next == null) { // This situation is possible when (K is multiple of list size) k == (n * size) of the list [1,2,3] k = 3,6,9 ... etc;
next = head; // So in that situation list will always come to it's original shape even after multiple rounds of rotation.
}
toBeFirstNode = next;
temp = next;
while (temp.next != null) {
temp = temp.next;
}
if (next != head) { // This situation is similar to the comment above, So (k = n * size);
temp.next = head; // if that is the case then we shouldn't be appending the second half to the first half, because then it will make go list round and round indefinitely.
}
head = toBeFirstNode;
printList(head);
}
/**
* 5 -> 10 -> 19 -> 28
* | | | |
* V V V V
* 7 20 22 35
* | | |
* V V V
* 8 50 40
* | |
* V V
* 30 45
* <p>
* Final Result = 5->7->8->10->19->20->22->28->30->35->40->45->50.
* <p>
* <p>
* We will use SortedMerge of Merge Sort to sort 2 list at a time and finally generate the final list
*
* @param head
*/
public void flattenList(Node head) {
Node temp = head;
Node prev = null;
Node current = null;
while (temp != null) {
if (prev == null) {
prev = getFlatList(temp);
} else {
current = getFlatList(temp);
prev = getSortedList(prev, current);
}
temp = temp.next;
}
printList(prev);
}
public Node getFlatList(Node head) {
LinkedListUtil flatList = new LinkedListUtil();
Node temp = head;
while (temp != null) {
flatList.append(temp.data);
temp = temp.down;
}
return flatList.head;
}
public Node getSortedList(Node firstHead, Node secondHead) {
LinkedListUtil sortedList = new LinkedListUtil();
Node t1 = firstHead;
Node t2 = secondHead;
while (t1 != null && t2 != null) {
if (t1.data < t2.data) {
sortedList.append(t1.data);
t1 = t1.next;
} else {
sortedList.append(t2.data);
t2 = t2.next;
}
}
while (t1 != null) {
sortedList.append(t1);
t1 = t1.next;
}
while (t2 != null) {
sortedList.append(t2);
t2 = t2.next;
}
return sortedList.head;
}
/**
* Add 1 to a number represented as linked list 2.5
* Number is represented in linked list such that each digit corresponds to a node in linked list.
* Add 1 to it. For example 1999 is represented as (1-> 9-> 9 -> 9) and adding 1 to it should change it to (2->0->0->0)
*/
public Node addOneToNumberRepresentedByListRecursively(Node head) {
int carry = addWithCarry(head);
if (carry > 0) {
Node carryLatestNode = new Node(carry);
carryLatestNode.next = head;
head = carryLatestNode;
}
return head;
}
/**
* This method is the utility method which will actually do the addition of 1 to the existing number and return
* if the No of digit will change after addition of 1. i.e 9 + 1--> 10, 99 + 1 ==> 100, 98 + 1 ==> 99
*
* @param head
* @return
*/
private int addWithCarry(Node head) {
if (head == null) {
return 1;
}
int result = head.data + addWithCarry(head.next);
head.data = result % 10;
return result / 10;
}
/**
* Iteratively Reverse a linked list using only 2 pointers (An Interesting Method)
* <p>
* Input : Head of following linked list
* 1->2->3->4->NULL
* Output : Linked list should be changed to,
* 4->3->2->1->NULL
* <p>
* Input : Head of following linked list
* 1->2->3->4->5->NULL
* Output : Linked list should be changed to,
* 5->4->3->2->1->NULL
*
* @param head
*/
public void reverseListWith2Pointers(Node head) {
Node current = head;
Node next;
while (current.next != null) {
next = current.next;
current.next = next.next;
next.next = head;
head = next;
}
printList(head);
}
/**
* Input: