-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.java
More file actions
446 lines (391 loc) · 11 KB
/
Copy pathLinkedList.java
File metadata and controls
446 lines (391 loc) · 11 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
public class LinkedList {
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;
// addFirst - O(1)
public void addFirst(int data) {
// step 1 = create new node
Node newNode = new Node(data);
size++;
if (head == null) { // when there does NOT exist any node
head = tail = newNode;
return;
}
// step 2 - newNode next = old head
newNode.next = head;// link
// step3 - head = newNode
head = newNode;
}
public void addLast(int data) {
Node newNode = new Node(data);
size++;
if (head == null) {
head = tail = newNode;
return;
}
tail.next = newNode;
tail = newNode;
}
public void add(int idx, int data) {
if (idx == 0) {
addFirst(data);
return;
}
Node newNode = new Node(data);
size++;
Node temp = head;
int i = 0;
while (i < idx - 1) {
temp = temp.next;
i++;
}
// i=idx-1; temp->prev
newNode.next = temp.next;// here temp is prev so temp.next is old next node
temp.next = newNode;
}
// printLL - O(n)
public void print() {
if (head == null) {
System.out.println("LinkedList is empty");
return;
}
Node temp = head;
System.out.print("head->");
while (temp != null) {
System.out.print(temp.data + "->");
temp = temp.next;
}
System.out.println("null");
}
public int removeFirst() {
if (size == 0) {
System.out.println("LinkedList is empty");
return Integer.MAX_VALUE;
} else if (size == 1) {
int val = head.data;
head = tail = null;
size = 0;
return val;
}
int val = head.data;
head = head.next;
size--;
return val;
}
public int removeLast() {
if (size == 0) {
System.out.println("LinkedList is empty");
return Integer.MAX_VALUE;
} else if (size == 1) {
int val = head.data;
head = tail = null;
size = 0;
return val;
}
Node prev = head;
for (int i = 0; i < size - 2; i++) {// size-1 is last index so previous to that is size-2;
prev = prev.next;
}
int val = tail.data;
prev.next = null;
tail = prev;
size--;
return val;
}
public int itrSearch(int key) { // O(n)
Node temp = head;
int i = 0;
while (temp != null) {
if (temp.data == key) {// key found
return i;
}
temp = temp.next;
i++;
}
return -1;// key not found
}
public int helper(Node head, int key) {
if (head == null) {
return -1;
}
if (head.data == key) {// key found
return 0;
}
int idx = helper(head.next, key);
if (idx == -1) {
return -1;
}
return idx + 1;
}
public int recSearch(int key) {
return helper(head, key);
}
public void reverse() { // O(n)
Node prev = null;
Node curr = tail = head;
Node next;
while (curr != null) {
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
head = prev;
}
public void deleteNthfromEnd(int n) {
int sz = 0;
Node temp = head;
while (temp != null) {
temp = temp.next;
sz++;
}
if (sz == n) {
head = head.next; // when we want to remove the first node i.e. n is equal to size of linked list.
return;
}
// sz-n
int i = 1;
int iToFind = sz - n;
Node prev = head;
while (i != iToFind) {
prev = prev.next;
i++;
}
prev.next = prev.next.next;
return;
}
// SLow-Fast Approach
public Node findMid(Node head) {
Node slow = head;
Node fast = head;
while (fast != null && fast.next != null) {
slow = slow.next; // +1
fast = fast.next.next; // +2
}
return slow; // slow is my mid
}
public boolean checkPalindrome() {
if (head == null || head.next == null) {
return true;
}
// step 1 - find mid
Node midNode = findMid(head);
// step 2 - reverse 2nd half
Node prev = null;
Node curr = midNode;
Node next;
while (curr != null) {
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
Node right = prev; // right half head
Node left = head; // left half head
// step 3 - check left half & right half
while (right != null) {
if (left.data != right.data) {
return false;
}
left = left.next;
right = right.next;
}
return true;
}
// LinkedList - 2
public static boolean isCycle() { // Floyd's Cycle Finding Algorithm
Node slow = head;
Node fast = head;
while (fast != null && fast.next != null) {
slow = slow.next; // +1
fast = fast.next.next; // +2
if (slow == fast) {
return true; // cycle exists
}
}
return false; // cycle doesn't exists
}
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 (slow == fast) {
cycle = true;
break;
}
}
if (cycle == false) {
return;
}
// find meeting point
slow = head;
Node prev = null; // last node
while (slow != fast) {
prev = fast;
slow = slow.next;
fast = fast.next;
}
// remove cycle -> last.next=null
prev.next = null;
}
private Node getMid(Node head) {
Node slow = head;
Node fast = head.next;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow; // slow = mid node
}
private Node merge(Node head1, Node head2) {
Node mergeLL = new Node(-1);
Node temp = mergeLL;
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) { // if any element is remining in leftHalf and the rightHalf is already done
temp.next = head1;
head1 = head1.next;
temp = temp.next;
}
while (head2 != null) {// if any element is remining in rightHalf and the leftHalf is already done
temp.next = head2;
head2 = head2.next;
temp = temp.next;
}
return mergeLL.next; // .next to avoid that -1 dummy Node
}
public Node mergeSort(Node head) {
if (head == null || head.next == null) {
return head;
}
// find mid
Node mid = getMid(head);
// left & right MS
Node rightHead = mid.next;
mid.next = null;
Node newLeft = mergeSort(head);
Node newRight = mergeSort(rightHead);
// merge
return merge(newLeft, newRight);
}
public void zigZag() {
// find mid
Node mid = getMid(head);
// 2nd half reverse
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; // this is left half head
Node right = prev; // this is right half head
Node nextL, nextR;
// Alernative merging
while (left != null && right != null) {
nextL = left.next;
left.next = right;
nextR = right.next;
right.next = nextL;
left = nextL;
right = nextR;
}
}
public static void main(String[] args) {
/*
* LinkedList ll = new LinkedList();
* ll.addFirst(2);
* ll.addFirst(1);
* ll.addLast(4);
* ll.addLast(5);
* ll.addLast(6);
* ll.addLast(7);
* ll.add(2, 3);
* ll.print();
* ll.removeFirst();
* ll.removeLast();
* ll.print();
* System.out.println("Size of LinkedList = " + ll.size);
* System.out.println("Key present at index = " + ll.itrSearch(3));
* System.out.println("Key present at index = " + ll.recSearch(4));
* // System.out.println(ll.recSearch(10));
* System.out.println(
* "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
* ll.print();
* ll.reverse();
* ll.print();
* ll.deleteNthfromEnd(2);
* ll.print();
* System.out.println(
* "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
* LinkedList ll2 = new LinkedList();
*
* ll2.addLast(1);
* ll2.addLast(2);
* ll2.addLast(2);
* ll2.addLast(1);
*
* ll2.print();
* System.out.println(ll2.checkPalindrome());
*
* head = new Node(1);
* Node temp = new Node(2);
* head.next = temp;
* head.next.next = new Node(3);
* head.next.next.next = temp;
* // 1->2->3->1
* System.out.println(isCycle());
* removeCycle();
* System.out.println(isCycle());
*
*
* LinkedList ll3 = new LinkedList();
* ll3.addFirst(1);
* ll3.addFirst(2);
* ll3.addFirst(3);
* ll3.addFirst(4);
* ll3.addFirst(5);
*
* // 5->4->3->2->1
*
* ll3.print();
* ll3.head = ll3.mergeSort(ll3.head); // O(nlogn)
* ll3.print();
*
*/
LinkedList ll4 = new LinkedList();
ll4.addLast(1);
ll4.addLast(2);
ll4.addLast(3);
ll4.addLast(4);
ll4.addLast(5);
ll4.print();
ll4.zigZag();
ll4.print();
}
}