-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathLinkedList.java
More file actions
514 lines (441 loc) · 9.39 KB
/
LinkedList.java
File metadata and controls
514 lines (441 loc) · 9.39 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
++package lecture9a15;
import java.util.ArrayList;
import java.util.Scanner;
public class LinkedList {
private class node {
int data;
node next;
}
private node head;
private node tail;
private int size;
//o(1)
public int getFirst() throws Exception {
if (this.size == 0)
throw new Exception("LL is Empty");
return this.head.data;
}
//O(1)
public int getLast() throws Exception {
if (this.size == 0)
throw new Exception("LL is Empty");
return this.tail.data;
}
//O(n)
public int getAt(int idx) throws Exception {
if (this.size == 0)
throw new Exception("LL is Empty");
if (idx > this.size || idx < 0)
throw new Exception("Invalid Index");
node temp = new node();
temp = head;
for (int i = 0; i < idx; i++) {
temp = temp.next;
}
return temp.data;
}
//O(1)
public void addLast(int item) {
node nn = new node();
nn.data = item;
nn.next = null;
// attach
if (this.size > 0)
tail.next = nn;
if (this.size == 0) {
this.head = nn;
this.tail = nn;
this.size++;
} else {
this.tail = nn;
this.size++;
}
}
//O(n)
public void display() {
node temp = head;
System.out.println("-------------------");
while (temp.next != null) {
System.out.println(temp.data);
temp = temp.next;
}
System.out.println(temp.data);
System.out.println(".");
System.out.println("---------------------");
}
//O(1)
public void addFirst(int item) {
node nn = new node();
nn.data = item;
nn.next = null;
// update
if (this.size == 0) {
this.head = nn;
this.tail = nn;
this.size++;
} else {
nn.next = head;
this.head = nn;
this.size++;
}
}
//O(n)
private node getNodeAt(int idx) throws Exception {
if (this.size == 0)
throw new Exception("LL is Empty");
if (idx > this.size || idx < 0)
throw new Exception("Invalid Index");
node temp = new node();
temp = head;
for (int i = 0; i < idx; i++) {
temp = temp.next;
}
return temp;
}
//O(n)
public void addAt(int idx, int item) throws Exception {
if (idx > this.size || idx < 0)
throw new Exception("Invalid Index");
if (idx == 0)
addFirst(item);
else if (idx == this.size)
addLast(item);
else {
node nn = new node();
node pn = getNodeAt(idx - 1);
nn.data = item;
nn.next = pn.next;
pn.next = nn;
this.size++;
}
}
//O(1)
public int removeFirst() throws Exception {
if (this.size == 0) {
throw new Exception("LL is Empty");
}
int rv = this.head.data;
if (this.size == 1) {
this.head = null;
this.tail = null;
this.size--;
} else {
this.head = head.next;
this.size--;
}
return rv;
}
// O(n)
public int removeLast() throws Exception {
if (this.size == 0) {
throw new Exception("LL is Empty");
}
int rv = this.tail.data;
if (this.size == 0) {
this.head = null;
this.tail = null;
this.size--;
} else {
node pn = getNodeAt(this.size - 2);
this.tail = pn;
pn.next = null;
this.size--;
}
return rv;
}
//O(n)
public int removeAt(int idx) throws Exception {
if (idx > this.size || idx < 0)
throw new Exception("Invalid Index");
if (idx == 0)
return removeFirst();
else if (idx == this.size - 1)
return removeLast();
else {
node pn = getNodeAt(idx - 1);
node cn = pn.next;
node nn = cn.next;
int rv = cn.data;
pn.next = nn;
this.size--;
return rv;
}
}
public void reverseDI() throws Exception {
node front = head;
for (int i = 1; i <= this.size / 2; i++) {
int temp = front.data;
node back = this.getNodeAt(this.size - i);
front.data = back.data;
back.data = temp;
front = front.next;
}
}
public void reversePI() {
node prevRef = head;
node curRef = head.next;
prevRef.next = null;
node temp;
for (int i = 1; i < this.size; i++) {
temp = curRef.next;
curRef.next = prevRef;
prevRef = curRef;
curRef = temp;
}
curRef = this.tail;
this.tail = this.head;
this.head = curRef;
}
public void reverseDR() {
reverseDR(this.head);
}
private node reverseDR(node Node) {
if (Node == null) {
return this.head;
}
int temp = Node.data;
Node = reverseDR(Node.next);
Node.data = temp;
return Node.next;
}
public void reversePR() {
reversePR(null, this.head);
node temp = this.head;
this.head = this.tail;
this.tail = temp;
}
private void reversePR(node prev, node cur) {
if (cur == null)
return;
reversePR(cur, cur.next);
cur.next = prev;
}
private class mover {
node left;
}
public void reverseDRHeap() {
mover HeapMover = new mover();
HeapMover.left = this.head;
reverseDRHeap(HeapMover, this.head, 0);
}
private void reverseDRHeap(mover HeapMover, node Right, int count) {
if (Right == null) {
return;
}
reverseDRHeap(HeapMover, Right.next, count + 1);
if (count >= this.size / 2) {
int temp = HeapMover.left.data;
HeapMover.left.data = Right.data;
Right.data = temp;
HeapMover.left = HeapMover.left.next;
}
}
public void fold() {
mover HeapMover = new mover();
HeapMover.left = this.head;
fold(HeapMover, this.head, 0);
}
private void fold(mover heapMover, node right, int i) {
if (right == null) {
return;
}
fold(heapMover, right.next, i + 1);
if (i >= this.size / 2) {
node temp = heapMover.left.next;
heapMover.left.next = right;
right.next = temp;
heapMover.left = temp;
}
if (i == this.size / 2) {
this.tail = right;
tail.next = null;
}
}
public int mid() {
return mid(this.head, this.head);
}
public int mid(node slow, node fast) {
if (fast == null || fast.next == null) {
return slow.data;
}
slow = slow.next;
fast = fast.next.next;
return mid(slow, fast);
}
private node midNode() {
node slow = this.head;
node fast = this.head;
while (fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
public int kFromEnd(int k) {
node slow = this.head;
node fast = this.head;
for (int i = 1; i <= k; i++)
fast = fast.next;
while (fast != null) {
slow = slow.next;
fast = fast.next;
}
return slow.data;
}
public void kreverse(int k) throws Exception {
LinkedList prev = null;
while (this.size > 0) {
LinkedList cur = new LinkedList();
for (int i = 1; i <= k; i++) {
cur.addFirst(this.removeFirst());
}
if (prev == null)
prev = cur;
else {
prev.tail.next = cur.head;
prev.size += cur.size;
prev.tail = cur.tail;
}
}
this.head = prev.head;
this.tail = prev.tail;
this.size = prev.size;
}
private LinkedList mergeTwoSortedLinkedList(LinkedList other) {
node temp = this.head;
node temp1 = other.head;
LinkedList sorted = new LinkedList();
while (temp != null && temp1 != null) {
if (temp.data < temp1.data) {
sorted.addLast(temp.data);
temp = temp.next;
} else {
sorted.addLast(temp1.data);
temp1 = temp1.next;
}
}
if (temp == null) {
while (temp1 != null) {
sorted.addLast(temp1.data);
temp1 = temp1.next;
}
}
if (temp1 == null) {
while (temp != null) {
sorted.addLast(temp.data);
temp = temp.next;
}
}
return sorted;
}
public void mergeSort() {
if (this.size == 1) {
return;
}
node midn = this.midNode();
node midnNext = midn.next;
LinkedList fh = new LinkedList();
fh.head = this.head;
fh.tail = midn;
fh.tail.next = null;
fh.size = (this.size + 1) / 2;
LinkedList sh = new LinkedList();
sh.head = midnNext;
sh.tail = this.tail;
sh.size = this.size / 2;
// get two smaller solved/sorted linked lists
fh.mergeSort();
sh.mergeSort();
// merge fh and sh
LinkedList merged = fh.mergeTwoSortedLinkedList(sh);
// update
this.head = merged.head;
this.tail = merged.tail;
this.size = merged.size;
}
public void DetectRemoveloop() {
node slow = this.head;
node fast = this.head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (fast == slow)
break;
}
if (fast == slow) {
node start = this.head;
node loop = slow;
while (start.next != loop.next) {
start = start.next;
loop = loop.next;
}
loop.next = null;
} else {
System.out.println("No Loop");
}
}
public void llDummy() {
node n1 = new node();
n1.data = 10;
node n2 = new node();
n2.data = 20;
node n3 = new node();
n3.data = 30;
node n4 = new node();
n4.data = 40;
node n5 = new node();
n5.data = 50;
node n6 = new node();
n6.data = 60;
n1.next = n2;
n2.next = n3;
n3.next = n4;
n4.next = n5;
n5.next = n6;
n6.next = n3;
this.head = n1;
this.tail = null;
this.size = 6;
}
public boolean palindrome() {
mover2 m = new mover2();
m.start = this.head;
return palindrome(m.start, this.head);
}
private class mover2{
node start ;
}
private boolean palindrome(node start, node end) {
if (end == null) {
return true;
}
if (palindrome(start, end.next)) {
if (start.data == end.data)
return true;
}
return false;
}
public void triplet(LinkedList one ,LinkedList two ,int sum){
this.mergeSort();
one.mergeSort();
two.mergeSort();
one.reverseDR();
node o1 = this.head;
while (o1 != null) {
node o2 = two.head;
node o3 = one.head;
while (o2!= null && o3 != null) {
int temp = o1.data + o2.data + o3.data;
if (temp > sum)
o3 = o3.next;
else if (temp < sum)
o2 = o2.next;
else {
System.out.println(o1.data + " " + o2.data + " " + o3.data);
return;
}
}
o1 = o1.next;
}
}
}