-
Notifications
You must be signed in to change notification settings - Fork 1
/
LinkedList1.java
752 lines (648 loc) · 19.2 KB
/
LinkedList1.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
public class LinkedList1 {
//Created single node in LL.
public static class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
public static Node head;
public static Node tail;
public static int size;
// Methods of Linked List:
//Adding Node at first place.
/*public void addFirst(int data) {
//Step 1: Create New Node.
Node newNode = new Node(data);
size++;
if (head == null) {
head = tail = newNode;
return;
}
//Step 2: newNode next = head (Linking Process)
newNode.next = head; //link
//Step 3: head = newNode.
head = newNode;
}*/
//Adding Node at Last Place.
/*public void addLast(int data) {
//step 1: Create New Node.
Node newNode = new Node(data);
size++;
if (head == null) {
head = tail = newNode;
return;
}
//Step 2: next of tail = newNode (Linking Process)
tail.next = newNode;
//Step 3: Make tail = newNode
tail = newNode;
}*/
//Adding in Middle of LL that is at given index.
/*public void add(int idx, int data) {
if (idx == 0) {
addFirst(data);
return;
}
size++;
Node newNode = new Node(data);
Node temp = head;
int i = 0;
while (i < idx - 1) {
temp = temp.next;
i++;
}
//i = idx-1, temp -> prev
newNode.next = temp.next;
temp.next = newNode;
}*/
//Removing First Node of LL.
public int removeFirst() {
//Special cases
if (size == 0) {
System.out.println("LL is Empty.");
return Integer.MIN_VALUE; //Returning Infinity value.
} else if (size == 1) {
int value = head.data;
head = tail = null;
size = 0;
return value;
}
int value = head.data;
head = head.next;
size--;
return value;
}
//Removing Last Node of LL.
public int removeLast() {
//Special Cases
if (size == 0) {
System.out.println("LL is Empty.");
return Integer.MIN_VALUE;
} else if (size == 1) {
int value = tail.data;
head = tail = null;
size = 0;
return value;
}
//Finding Previous Node of Tail: i = size - 2;
Node prev = head;
for (int i = 0; i < size - 2; i++) {
prev = prev.next;
}
int value = prev.next.data; //tail.data;
prev.next = null;
tail = prev;
size--;
return value;
}
//Search in LL: Iterative Approach. O(n)
public int iterativeSearch(int key) {
Node temp = head;
int i = 0;
while (temp != null) {
//If Key found then return Index.
if (temp.data == key) {
return i;
}
temp = temp.next;
i++;
}
//Key Not found.
return -1;
}
//Search in LL: Recursive Approach. O(n)
public int recursiveSearch(int key) {
return helper(head, key);
}
//TC = O(n) and SC = O(n)
public int helper(Node head, int key) {
//Base Case
if (head == null) {
return -1;
}
//Recursion/work
if (head.data == key) {
return 0;
}
//While Backtracking.
int idx = helper(head.next, key);
if (idx == -1) {
return -1;
}
return idx + 1;
}
//Reverse a LL: Iterative Approach O(n)
public void reverseLL() {
Node prev = null;
Node curr = tail = head;
Node next;
while (curr != null) {
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
head = prev;
}
//Find & Remove Nth Node from End: Iterative Approach O(n)
public void deleteNthFromEnd(int n) {
//calculate size
int size = 0;
Node temp = head;
while (temp != null) {
temp = temp.next;
size++;
}
//Remove first Node operation
if (n == size) {
head = head.next;
return;
}
//Finding Node size - n
int i = 1, iToFind = size - n;
Node prev = head;
while (i < iToFind) {
prev = prev.next;
i++;
}
prev.next = prev.next.next;
}
//Check if LL is Palindrome or Not.
public boolean checkPalindrome() {
//Base Case
if (head == null || head.next == null) {
return true;
}
//step 1: Find Middle Node
Node midNode = findMiddle(head);
//step 2: Reverse 2nd Half from Middle.
Node prev = null;
Node curr = midNode;
Node next;
while (curr != null) {
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
//step 3: Compare Left Half == Right Half.
Node right = prev; //Right Half Head.
Node left = head; //Left Half Head.
while (right != null) {
if (left.data != right.data) {
return false;
}
left = left.next;
right = right.next;
}
return true;
}
//Slow-Fast Approach to find Middle Node. (Helper Function for checkPalindrome() method)
public Node findMiddle(Node head) {
Node slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next; //+1
fast = fast.next.next; //+2
}
return slow; //slow is middle node.
}
//Detect Cyclic/Loop in LL using Floyd's Cyclic Find Algorithm.
public static boolean isCycle() {
Node slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next; //+1
fast = fast.next.next; //+2
if (slow == fast) {
return true; //cycle exist.
}
}
return false; //cycle doesn't exist.
}
//Remove Cycle/Loop in LL.
/*Note: This code is not written 4 full cyclic LL. (Full cyclic means the last node of LL pointing towards head node)
If you want code 4 cyclic LL then change the initialization of `prev` (Node prev = head)*/
public static void removeCycle() {
//Detect cycle
Node slow = head;
Node fast = head;
boolean cycle = false;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (fast == slow) {
cycle = true;
break;
}
}
if (!cycle) return;
//Find meeting point
slow = head;
Node prev = null; //Last Node
while (fast != slow) {
prev = fast;
slow = slow.next;
fast = fast.next;
}
// remove cycle -> last.next = null
prev.next = null;
}
//Printing the LL.
public static void printLL() {
if (head == null) {
System.out.println("LL is Empty.");
return;
} //Removing this case will result in printing "Null" when the LL is empty.
Node temp = head;
while (temp != null) {
System.out.print(temp.data + "-> ");
temp = temp.next;
}
System.out.println("Null");
}
//Merge Sort on LL O(nLog n)
/*public Node mergeSort(Node head) {
//Base Case
if (head == null || head.next == null) {
return head;
}
//Find Mid
Node mid = getMid(head);
//Call MergeSort for Left Half & Right Half
Node rightHead = mid.next;
mid.next = null;
Node newLeft = mergeSort(head);
Node newRight = mergeSort(rightHead);
//Merge
return merge(newLeft, newRight);
}*/
private Node getMid(Node head) {
Node slow = head, fast = head.next;
while (fast != null && fast.next != null) {
slow = slow.next; //+1
fast = fast.next.next; //+2
}
return slow; //Mid Node
}
/*private Node merge(Node head1, Node head2) {
Node mergedLL = new Node(-1);
Node temp = mergedLL;
while (head1 != null && head2 != null) {
if (head1.data <= head2.data) {
temp.next = head1;
head1 = head1.next;
temp = temp.next;
} else {
temp.next = head2;
head2 = head2.next;
temp = temp.next;
}
}
while (head1 != null) {
temp.next = head1;
head1 = head1.next;
temp = temp.next;
}
while (head2 != null) {
temp.next = head2;
head2 = head2.next;
temp = temp.next;
}
return mergedLL.next;
}*/
//Zig-Zag Linked List.
public void zigZag() {
//Find Middle Node.
Node slow = head;
Node fast = head.next;
while (fast != null && fast.next != null) {
slow = slow.next; //+1
fast = fast.next.next; //+2
}
Node mid = slow;
//Reverse the 2nd Half.
Node curr = mid.next;
mid.next = null;
Node prev = null;
Node next;
while (curr != null) {
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
Node left = head;
Node right = prev;
Node nextL, nextR;
//Alternate Merge (zigzag merge)
while (left != null && right != null) {
nextL = left.next; //Zig-Zag Code
left.next = right;
nextR = right.next;
right.next = nextL;
right = nextR; //Update Loop Code
left = nextL;
}
}
//Question 1: Intersection of Two Linked Lists.
public Node getIntersectionNode(Node head1, Node head2) {
while (head2 != null) {
Node temp = head1;
while (temp != null) {
if (temp == head2) {
return head2;
}
temp = temp.next;
}
head2 = head2.next;
}
return null;
}
//Question 2: Delete N Nodes After M Nodes of a Linked List.
/*static Node push(Node head_ref, int new_data) {
Node new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}*/
static void printList(Node head) {
Node temp = head;
while (temp != null) {
System.out.printf("%d ", temp.data);
temp = temp.next;
}
System.out.printf("\n");
}
static void skipMdeleteN(Node head, int M, int N) {
Node curr = head, t;
int count;
while (curr != null) {
for (count = 1; count < M && curr != null; count++)
curr = curr.next;
if (curr == null)
return;
t = curr.next;
for (count = 1; count <= N && t != null; count++) {
Node temp = t;
t = t.next;
}
curr.next = t;
curr = t;
}
}
//Question 3: Swapping Nodes in a Linked List.
public void swapNodes(int x, int y) {
if (x == y)
return;
Node prevX = null, currX = head;
while (currX != null && currX.data != x) {
prevX = currX;
currX = currX.next;
}
Node prevY = null, currY = head;
while (currY != null && currY.data != y) {
prevY = currY;
currY = currY.next;
}
if (currX == null || currY == null)
return;
if (prevX != null)
prevX.next = currY;
else
head = currY;
if (prevY != null)
prevY.next = currX;
else
head = currX;
Node temp = currX.next;
currX.next = currY.next;
currY.next = temp;
}
public void push(int new_data) {
Node new_Node = new Node(new_data);
new_Node.next = head;
head = new_Node;
}
//Question 4: Odd Even Linked List.
void segregateEvenOdd() {
Node end = head;
Node prev = null;
Node curr = head;
while (end.next != null) end = end.next;
Node new_end = end;
while (curr.data % 2 != 0 && curr != end) {
new_end.next = curr;
curr = curr.next;
new_end.next.next = null;
new_end = new_end.next;
}
if (curr.data % 2 == 0) {
head = curr;
while (curr != end) {
if (curr.data % 2 == 0) {
prev = curr;
curr = curr.next;
} else {
prev.next = curr.next;
curr.next = null;
new_end.next = curr;
new_end = curr;
curr = prev.next;
}
}
} else prev = curr;
if (new_end != end && end.data % 2 != 0) {
prev.next = end.next;
end.next = null;
new_end.next = end;
}
}
//Question 5: Merge k Sorted Lists.
public static Node SortedMerge(Node a, Node b) {
Node result = null;
if (a == null)
return b;
else if (b == null)
return a;
if (a.data <= b.data) {
result = a;
result.next = SortedMerge(a.next, b);
} else {
result = b;
result.next = SortedMerge(a, b.next);
}
return result;
}
public static Node mergeKLists(Node[] arr, int last) {
while (last != 0) {
int i = 0, j = last;
while (i < j) {
arr[i] = SortedMerge(arr[i], arr[j]);
i++;
j--;
if (i >= j) last = j;
}
}
return arr[0];
}
public static void main(String[] para_coder) {
LinkedList1 ll = new LinkedList1();
// ll.addFirst(1);
// ll.addFirst(2);
// ll.addFirst(2);
// ll.addFirst(1);
// ll.add(2, 3);
// ll.printLL(); //1->2->2->1->null
// System.out.println("LL size = " + size);
// ll.removeFirst();
// ll.printLL();
// ll.removeLast();
// ll.printLL();
// System.out.println("LL size = " + size);
// System.out.println(ll.recursiveSearch(3));
// System.out.println(ll.recursiveSearch(10));
// ll.reverseLL();
// ll.printLL();
// ll.deleteNthFromEnd(3);
// ll.printLL();
// head = new Node(1);
// Node temp = new Node(2);
// head.next = temp;
// head.next.next = new Node(3);
// head.next.next.next = new Node(4);
// head.next.next.next.next = temp;
// //1-> 2-> 3-> 4-> 2
// System.out.println(isCycle());
// removeCycle();
// System.out.println(isCycle());
//Create LL
// LinkedList<Integer> ll = new LinkedList<>();
//
// //Add into LL
// ll.addLast(1);
// ll.addLast(2);
// ll.addFirst(0); //0->1->2
//
// System.out.println(ll); //Printing the LL
//
// //Remove from LL
// ll.removeLast();
// ll.removeFirst();
//
// System.out.println(ll); //Printing the LL
//Creating LL
// ll.addLast(1);
// ll.addLast(2);
// ll.addLast(3);
// ll.addLast(4);
// ll.addLast(5);
// ll.addLast(6);
// //1->2->3->4->5->6->Null
//
// ll.printLL();
// ll.zigZag();
// ll.printLL();
/*Assignment Question:
Question 1: Intersection of Two Linked Lists.
Time Complexity : o(m*n)
Space Complexity: o(1)*/
/*Node head1, head2;
head1 = new Node(10);
head2 = new Node(3);
Node newNode = new Node(6);
head2.next = newNode;
newNode = new Node(9);
head2.next.next = newNode;
newNode = new Node(15);
head1.next = newNode;
head2.next.next.next = newNode;
newNode = new Node(30);
head1.next.next = newNode;
head1.next.next.next = null;
Node intersectionPoint = ll.getIntersectionNode(head1, head2);
if (intersectionPoint == null) {
System.out.print(" No Intersection Point \n");
} else {
System.out.print("Intersection Point: " + intersectionPoint.data);
}*/
/*Question 2: Delete N Nodes After M Nodes of a Linked List.
Time Complexity : o(n)
Space Complexity: o(1)*/
/*Node head = null;
int M = 2, N = 3;
head = push(head, 10);
head = push(head, 9);
head = push(head, 8);
head = push(head, 7);
head = push(head, 6);
head = push(head, 5);
head = push(head, 4);
head = push(head, 3);
head = push(head, 2);
head = push(head, 1);
System.out.printf("M = %d, N = %d \n" + "Input Linked list: ", M, N);
printList(head);
skipMdeleteN(head, M, N);
System.out.printf("Output Linked list: ");
printList(head);*/
/*Question 3: Swapping Nodes in a Linked List.
Time Complexity : o(n)
Space Complexity: o(1)*/
/*ll.push(7);
ll.push(6);
ll.push(5);
ll.push(4);
ll.push(3);
ll.push(2);
ll.push(1);
int x = 4, y = 3;
System.out.printf("X = %d, Y = %d\n", x, y);
System.out.print("Linked list before Swapping: ");
ll.printList();
ll.swapNodes(x, y); //Swap node x=4 and y=3.
System.out.print("\nLinked list after Swapping: ");
ll.printList();*/
/*Question 4: Odd Even Linked List.
Time Complexity : o(n)
Space Complexity: o(1)*/
/*ll.push(11);
ll.push(10);
ll.push(8);
ll.push(6);
ll.push(4);
ll.push(2);
ll.push(0);
System.out.print("Input Linked List: ");
ll.printLL();
ll.segregateEvenOdd();
System.out.print("Output: Updated Linked List: ");
ll.printLL();*/
/*Question 5: Merge k Sorted Lists.
Time Complexity : o(n log k)
Space Complexity: o(n)*/
int k = 3, n = 4;
Node[] arr = new Node[k];
//First LL
arr[0] = new Node(1);
arr[0].next = new Node(3);
arr[0].next.next = new Node(5);
arr[0].next.next.next = new Node(7);
//Second LL
arr[1] = new Node(2);
arr[1].next = new Node(4);
arr[1].next.next = new Node(6);
arr[1].next.next.next = new Node(8);
//Third LL
arr[2] = new Node(0);
arr[2].next = new Node(9);
arr[2].next.next = new Node(10);
arr[2].next.next.next = new Node(11);
//Merge LL
System.out.print("After Merging LL: ");
Node head = mergeKLists(arr, k - 1);
printList(head);
}
}