-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNode.java
538 lines (482 loc) · 13.8 KB
/
Node.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
import java.util.ArrayList;
public class Node {
int data;
Node next;
Node back;
Node(int data1, Node next1, Node back1) {
this.data = data1;
this.next = next1;
this.back = back1;
}
Node(int data1) {
this.data = data1;
this.next = null;
this.back = null;
}
}
class doublylinkedlist {
public static Node convertarrtoDLL(int[] arr) {
Node head = new Node(arr[0]);
Node prev = head;
for (int i = 1; i < arr.length; i++) {
Node temp = new Node(arr[i], null, null);
temp.back = prev;
prev.next = temp;
prev = temp;
}
return head;
}
public static void printDLL(Node head) {
Node temp = head;
while (temp != null) {
System.out.print(temp.data + " ");
temp = temp.next;
}
}
// delete head of linked list
public static Node deletehead(Node head) {
if (head == null) {
return null;
}
if (head.next == null) {
return null;
}
Node prev = head;
head = head.next;
head.back = null;
prev.next = null;
return head;
}
public static Node deletetail(Node head) {
if (head == null) {
return null;
}
if (head.next == null) {
return null;
}
Node temp = head;
while (temp.next.next != null) {
temp = temp.next;
}
Node prev = temp;
temp = temp.next;
temp.back = null;
temp.next = null;
prev.next = null;
return head;
}
// Delete kth Element of doubly linked list
public static Node deletekth(Node head, int k) {
int count = 0;
Node temp = head;
if (k == 1) {
head = deletehead(head);
} else {
while (temp != null) {
count++;
if (count == k) {
Node prev = temp.back;
Node front = temp.next;
if (prev == null && front == null) {
return null;
} else if (prev == null) {
temp = temp.next;
temp.next.back = null;
return head;
} else if (front == null) {
temp.back.next = null;
temp.back = null;
return head;
} else {
prev.next = temp.next;
front.back = prev;
}
} else {
temp = temp.next;
}
}
}
return head;
}
public static Node insertAtEnd(Node head, int x) {
Node temp = head;
Node newnode = new Node(x);
while (temp != null) {
temp = temp.next;
}
temp = newnode;
return head;
}
public static Node deleteNode(Node head, int x) {
Node temp = head;
int count = 0;
while (temp != null) {
count++;
if (count == x) {
Node prev = temp.back;
Node front = temp.next;
prev.next = temp.next;
front.back = temp.back;
break;
} else {
temp = temp.next;
}
}
return head;
}
// insert before head
public static Node insertathead(Node head, int target) {
Node newnode = new Node(target);
Node temp = head;
newnode.next = temp;
temp.back = newnode;
return newnode;
}
public static Node insertbeforetail(Node head, int k) {
Node newNode = new Node(k);
Node temp = head;
if (head.next == null) {
Node makehead = new Node(k);
makehead.next = head;
head.back = makehead;
return makehead;
}
while (temp.next.next != null) {
temp = temp.next;
}
Node front = temp.next;
temp.next = newNode;
newNode.back = temp;
front.back = newNode;
newNode.next = front;
return head;
}
public static Node insertatk(Node head, int k, int target) {
int count = 0;
Node temp = head;
// Edge cases 1. if(k==1)
if (k == 1) {
Node newNode = new Node(target, head, null);
head.back = newNode;
return newNode;
}
while (temp != null) {
count++;
if (count == (k - 1)) {
Node front = temp.next;
Node newNode = new Node(target, front, temp);
temp.next = newNode;
front.back = newNode;
return head;
}
temp = temp.next;
}
return null;
}
public static Node insertafterk(Node head, int p, int x) {
Node temp = head;
int count = 0;
while (temp != null) {
if (count == p) { // Fix: Directly compare with p (0-based index)
Node front = temp.next;
Node newNode = new Node(x);
newNode.next = front;
newNode.back = temp;
temp.next = newNode;
if (front != null) { // Fix: Avoid NullPointerException
front.back = newNode;
}
return head;
}
temp = temp.next;
count++;
}
return head;
}
// reverse a DLL
public static Node reverseaDLL(Node head) {
Node temp = head;
Node last = null;
if (temp.next == null) {
return temp;
}
while (temp != null) {
last = temp.back;
temp.back = temp.next;
temp.next = last;
temp = temp.back;
}
return last.back;
}
public static Node add2ll(Node head, Node head2) {
int count = 0;
double sum = 0;
Node temp = head;
int count2 = 0;
double sum2 = 0;
Node temp2 = head2;
while (temp != null) {
sum = sum + (temp.data * Math.pow(10, count));
count++;
temp = temp.next;
}
while (temp2 != null) {
sum2 = sum2 + (temp2.data * Math.pow(10, count2));
count2++;
temp2 = temp2.next;
}
int total = (int) (sum + sum2);
int first = total % 10;
total = total / 10;
Node newNode = new Node(first);
Node temp3 = newNode;
while (total > 0) {
int test = total % 10;
total = total / 10;
Node latest = new Node(test, null, temp3);
temp3.next = latest;
temp3 = temp3.next;
}
return newNode;
}
public static Node add2lleff(Node head, Node head2) {
Node temp = head;
Node temp2 = head2;
Node DummyNode = new Node(-1);
Node current = DummyNode;
int carry = 0;
int sum = 0;
while (temp != null || temp2 != null) {
sum = carry + ((temp != null) ? temp.data : 0) + ((temp2 != null) ? temp2.data : 0);
Node newNode = new Node(sum % 10);
carry = sum / 10;
current.next = newNode;
current = current.next;
temp = (temp != null) ? temp.next : temp;
temp2 = (temp2 != null) ? temp2.next : temp2;
}
if (carry != 0) {
Node newNode = new Node(carry);
current.next = newNode;
}
return DummyNode.next;
}
public static Node oddEvenBrute(Node head) {
Node temp = head;
ArrayList<Integer> oddeven = new ArrayList<>();
while (temp != null && temp.next != null) {
oddeven.add(temp.data);
temp = temp.next.next;
}
if (temp != null) {
oddeven.add(temp.data);
}
temp = head.next;
while (temp != null && temp.next != null) {
oddeven.add(temp.data);
temp = temp.next.next;
}
if (temp != null) {
oddeven.add(temp.data);
}
temp = head;
int i = 0;
while (temp != null) {
temp.data = oddeven.get(i);
i++;
temp = temp.next;
}
return head;
}
public static Node oddEvenEff(Node head) {
if (head == null || head.next == null) {
return head;
}
Node odd = head;
Node even = head.next;
Node evenhead = head.next;
while (even != null && even.next != null) {
odd.next = odd.next.next;
even.next = even.next.next;
odd = odd.next;
even = even.next;
}
odd.next = evenhead;
return head;
}
public static Node sort012brute(Node head) {
int zero = 0;
int one = 0;
int two = 0;
Node temp = head;
if (head == null) {
return head;
}
while (temp != null) {
if (temp.data == 0) {
zero++;
} else if (temp.data == 1) {
one++;
} else {
two++;
}
temp = temp.next;
}
temp = head;
while (temp != null) {
if (zero != 0) {
temp.data = 0;
zero--;
} else if (one != 0) {
temp.data = 1;
one--;
} else if (two != 0) {
temp.data = 2;
two--;
}
temp = temp.next;
}
return head;
}
public static Node sort012eff(Node head) {
Node zeroHead = new Node(-1);
Node oneHead = new Node(-1);
Node twoHead = new Node(-1);
Node zero = zeroHead;
Node one = oneHead;
Node two = twoHead;
if (head == null || head.next == null) {
return head;
}
Node temp = head;
while (temp != null) {
if (temp.data == 0) {
zero.next = temp;
zero = temp;
} else if (temp.data == 1) {
one.next = temp;
one = temp;
} else {
two.next = temp;
two = temp;
}
temp = temp.next;
}
zero.next = (oneHead.next != null) ? oneHead.next : (twoHead.next != null) ? twoHead.next : null;
one.next = twoHead.next;
two.next = null;
return zeroHead.next;
}
// remove nth brute
public static Node removeNbrute(Node head, int n) {
int count = n;
int length = 0;
Node temp = head;
if (head == null) {
return null;
}
while (temp != null) {
length++;
temp = temp.next;
}
if (count == length) {
return head.next;
}
int res = length - count;
temp = head;
System.out.println(length);
System.out.println(count);
System.out.println(res);
int count2 = 0;
while (temp != null) {
count2++;
if (count2 == res) {
temp.next = temp.next.next;
break;
}
temp = temp.next;
}
return head;
}
public static Node removeNeff(Node head, int n) {
Node fast = head;
Node slow = head;
for (int i = 0; i < n; i++) {
fast = fast.next;
}
if (fast == null) {
return head.next;
}
while (fast.next != null) {
fast = fast.next;
slow = slow.next;
}
slow.next = slow.next.next;
return head;
}
public static Node reverseLL(Node head) {
Node temp = head;
while (temp != null) {
}
return head;
}
public static Node removeDuplicates(Node head) {
Node temp = head;
while (temp != null && temp.next != null) {
Node next = temp.next;
if (temp.data == next.data) {
temp.next = next.next;
// next = next.next;
} else {
temp = next;
}
}
return head;
}
public static Node deleteallkeys(Node head, int x) {
if (head == null) {
return null;
}
while (head != null && head.data == x) {
head = head.next;
}
if (head == null) {
return null;
}
Node temp = head;
while (temp != null && temp.next != null) {
if (temp.next.data == x) {
temp.next = temp.next.next;
} else {
temp = temp.next;
}
}
return head;
}
public static void main(String[] args) {
int[] arr = { 2, 2, 2, 2, 2, };
// int[] arr2 = { 4, 5, 9, 9, 5 };
Node head = convertarrtoDLL(arr);
// Node head2 = convertarrtoDLL(arr2);
// head = deletehead(head);
// head = deletetail(head);
// head = deletekth(head, 1);
// head = insertAtEnd(head, 8);
// head = deleteNode(head, 1);
// head = insertathead(head, 25);
// head = insertbeforetail(head, 1502);
// head = insertatk(head, 6, 25);
// head = insertafterk(head, 2, 6);
// head = reverseaDLL(head);
// System.out.println(add2ll(head, head2));
// head = add2lleff(head, head2);
// head = oddEvenEff(head);
// head = sort012eff(head);
// head = removeNeff(head, 5);
// head = reverseLL(head);
// head = removeDuplicates(head);
head = deleteallkeys(head, 2);
printDLL(head);
// printDLL(head2);
}
}