-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinked_List.java
530 lines (382 loc) · 13.4 KB
/
Linked_List.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
package DS;
import java.util.Scanner;
public class Linked_List {
public static void enterNext(){
System.out.println("Enter next element");
}
public static Node<Integer> takeLinkedListInput() {
Node<Integer> head = null,
prevNode = null;
enterNext();
Scanner s = new Scanner(System.in);
int input = s.nextInt();
while (input != -1) {
Node<Integer> newNode = new Node<>(input, null);
if(head == null)
head = newNode;
else prevNode.next = newNode;
prevNode = newNode;
enterNext();
input = s.nextInt();
}
return head;
}
public static void printLinkedList(Node<Integer> input) {
Node<Integer> currentNode = input;
while (currentNode != null) {
System.out.print(currentNode.data + " ");
currentNode = currentNode.next;
}
System.out.println();
}
public static Node<Integer> insertNode(Node<Integer> head, int newElement, int position){
Node<Integer> newNode = new Node<>(newElement);
if (position == 0) {
newNode.next = head;
return newNode;
}
int currentPosition = 1;
Node<Integer> temp = head;
while (currentPosition < position) {
temp = temp.next;
currentPosition++;
}
newNode.next = temp.next;
temp.next = newNode;
return head;
}
public static Node<Integer> deleteNode(Node<Integer> head, int position) {
if(position == 0)
return head.next;
int currentPosition = 1;
Node<Integer> temp = head;
while(currentPosition < position) {
temp = temp.next;
currentPosition++;
}
temp.next = temp.next.next;
return head;
}
public static Node<Integer> swap(Node<Integer> head, int i, int j) {
if(i == 0){
Node<Integer> ith = head,
prevj = null,
jth = head;
int count = 0;
while(count < j){
prevj = jth;
jth = jth.next;
count++;
}
head = jth;
Node<Integer> nexti = ith.next,
nextj = jth.next;
if(i == (j - 1)){
jth.next = ith;
ith.next = nextj;
}
else{
ith.next = nextj;
prevj.next = ith;
jth.next = nexti;
}
return head;
}
Node<Integer> previ = null,
ith = head;
int count = 0;
while(count < i){
previ = ith;
ith = ith.next;
count++;
}
Node<Integer> prevj = null,
jth = head;
count = 0;
while(count < j) {
prevj = jth;
jth = jth.next;
count++;
}
Node<Integer> nexti = ith.next,
nextj = jth.next;
if(i != (j - 1)){
previ.next = jth;
prevj.next = ith;
ith.next = nextj;
jth.next = nexti;
}
else{
previ.next = jth;
ith.next = nextj;
jth.next = ith;
}
return head;
}
public static int findMinPosition(Node<Integer> head) {
int minPosition = 0,
currentPosition = 0;
Node<Integer> minNode = head,
temp = head;
while(temp != null){
if(temp.data < minNode.data){
minPosition = currentPosition;
minNode = temp;
}
currentPosition++;
temp = temp.next;
}
return minPosition;
}
public static boolean headCheck(Node<Integer> head){ return ((head == null) || (head.next == null)); }
public static Node<Integer> InsertionSort(Node<Integer> head) {
if (headCheck(head))
return head;
Node<Integer> sortedSoFar = head,
remaining = head.next;
head.next = null;
while (remaining != null) {
Node<Integer> currentNode = remaining;
remaining = remaining.next;
Node<Integer> temp = sortedSoFar,
prevTemp = null;
while(temp != null){
if (temp.data > currentNode.data) {
if (prevTemp == null) {
currentNode.next = sortedSoFar;
sortedSoFar = currentNode;
}
else {
currentNode.next = temp;
prevTemp.next = currentNode;
}
break;
}
prevTemp = temp;
temp = temp.next;
}
if (temp == null) {
prevTemp.next = currentNode;
currentNode.next = null;
}
}
return sortedSoFar;
}
public static Node<Integer> selectionSortRecursive(Node<Integer> head) {
if (headCheck(head))
return head;
int minP = findMinPosition(head);
head = swap(head, 0, minP);
head.next = selectionSortRecursive(head.next);
return head;
}
public static void printReverse(Node<Integer> head) {
if(head == null)
return;
printReverse(head.next);
System.out.print(head.data + " ");
}
Node<Integer> ReverseRecursively(Node head) {
if(headCheck(head))
return head;
Node<Integer> smallerHead = ReverseRecursively(head.next),
temp = smallerHead;
while(temp.next != null)
temp = temp.next;
temp.next = head;
head.next = null;
return smallerHead;
}
// Recursive reversion using class DoubleNode
public static DoubleNode reverseRecursive3(Node<Integer> head) {
if (headCheck(head)) {
DoubleNode output = new DoubleNode(head, head);
return output;
}
DoubleNode smallerOutput = reverseRecursive3(head.next);
smallerOutput.tail.next = head;
head.next = null;
DoubleNode output = new DoubleNode(smallerOutput.head, head);
return output;
}
public static Node<Integer> reverseRecursive2(Node<Integer> head) {
if (headCheck(head))
return head;
Node<Integer> smallerHead = reverseRecursive2(head.next);
head.next.next = head;
head.next = null;
return smallerHead;
}
public static Node<Integer> reverseRecursive(Node<Integer> head) {
if (headCheck(head))
return head;
Node<Integer> smallerHead = reverseRecursive(head.next),
temp = smallerHead;
while(temp.next != null)
temp = temp.next;
temp.next = head;
head.next = null;
return smallerHead;
}
// Detect and remove loop from linked list
public static int detectAndRemoveLoop(Node<Integer> head){
if(headCheck(head))
return -1;
Node<Integer> slow = head,
fast = head.next;
while (slow != null && fast != null && fast.next != null){
slow = slow.next;
fast = fast.next.next;
if (slow == fast){
removeLoop(slow, head);
return 1;
}
}
return -1;
}
public static void removeLoop(Node<Integer> loop, Node<Integer> head){
Node<Integer> p1 = loop,
p2 = loop;
int k = 1,
i;
while(p1.next != p2){
p1 = p1.next;
k++;
}
p1 = head;
p2 = head;
for (i = 0; i < k; i++)
p2 = p2.next;
while (p2 != p1){
p1 = p1.next;
p2 = p2.next;
}
p2 = p2.next;
while (p2.next != p1)
p2 = p2.next;
p2.next = null;
}
// Merge Sort for Linked List
/* Similar to mid point function for array for merge sort */
public static Node getMiddle(Node head){
if(headCheck(head))
return head;
Node fast = head.next,
slow = head;
while(fast != null){
fast = fast.next;
if(fast != null){
fast = fast.next;
slow = slow.next;
}
}
return slow;
}
/* Merge function for two soreted Linked Lists */
public static Node<Integer> merge(Node<Integer> a, Node<Integer> b){
Node result = null;
// Base cases
if(a == null && b == null) return null;
else if(a == null) return b;
else if(b == null) return a;
if(a.data <= b.data){
result = a;
result.next = merge(a.next, b);
}
else if (a.data > b.data){
result = b;
result.next = merge(a, b.next);
}
return result;
}
/* Merge Sort function */
public static Node mergeSort(Node<Integer> head){
if(headCheck(head))
return head;
Node<Integer> midNode = getMiddle(head), /* End of left half linked list */
nextOfMidNode = midNode.next; /* Start of right half linked list */
midNode.next = null; /* Effectively dividing linked list into two parts */
Node<Integer> left = mergeSort(head),
right = mergeSort(nextOfMidNode),
sortedList = merge(left, right);
return sortedList;
}
public static Node addTwoLists(Node<Integer> a, Node<Integer> b){
if(a == null && b == null) return null;
else if (a == null) return b;
else if (b == null) return a;
Node<Integer> temp,
result = null,
prev = null;
int carry = 0,
sum;
while(a != null || b != null){
sum = carry + (a != null ? a.data : 0) + (b != null ? b.data : 0);
carry = (sum >= 10) ? 1 : 0;
sum %= 10;
temp = new Node<>();
temp.data = sum;
if(result == null)
result = temp;
else
prev.next = temp;
prev = temp;
if(a != null)
a = a.next;
if(b != null)
b = b.next;
if(carry > 0){
temp.next = new Node<>();
temp.data = carry;
}
}
return result;
}
public static Node<Integer> rotate(Node<Integer> head, int k) {
if (k == 0)
return head;
// Let us understand the below code for example k = 4 and list = 10->20->30->40->50->60.
Node current = head;
// current will either point to kth or NULL after this loop. current will point to node 40 in the above example
int count = 1;
while (count < k && current != null) {
current = current.next;
count++;
}
// If current is NULL, k is greater than or equal to count
// of nodes in linked list. Don't change the list in this case
if (current == null)
return head;
// current points to kth node. Store it in a variable.
// kthNode points to node 40 in the above example
Node kthNode = current;
// current will point to last node after this loop
// current will point to node 60 in the above example
while (current.next != null)
current = current.next;
// Change next of last node to previous head
// Next of 60 is now changed to node 10
current.next = head;
// Change head to (k+1)th node
// head is now changed to node 50
head = kthNode.next;
// change next of kth node to null
kthNode.next = null;
return head;
}
public static void main(String[] args) {
Node<Integer> input = takeLinkedListInput();
printLinkedList(input);
// printReverse(input);
//
// input = InsertionSort(input);
// printLinkedList(input);
//
// printLinkedList(mergeSort(input));
// System.out.println(detectAndRemoveLoop(input));
// printLinkedList(addTwoLists(input, input));
Node<Integer> head = rotate(input, 3);
printLinkedList(head);
printLinkedList(reverseRecursive3(input).head);
}
}