forked from hariom20singh/cargame1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.java
More file actions
387 lines (333 loc) · 8.64 KB
/
LinkedList.java
File metadata and controls
387 lines (333 loc) · 8.64 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
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;
public void addFirst(int data) {
//step1 = create new node
Node newNode = new Node(data);
size++;
if(head == null) {
head = tail = newNode;
return;
}
//step2 - newNode next = 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 print() { // O(n)
Node temp = head;
while(temp != null) {
System.out.print(temp.data+"->");
temp = temp.next;
}
System.out.println("null");
}
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;
temp.next = newNode;
}
public int removeFirst() {
if(size == 0) {
System.out.println("LL is empty");
return Integer.MIN_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("LL is empty");
return Integer.MIN_VALUE;
} else if(size == 1) {
int val = head.data;
head = tail = null;
size = 0;
return val;
}
//prev : i = size-2
Node prev = head;
for(int i=0; i<size-2; i++) {
prev = prev.next;
}
int val = prev.next.data; //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++;
}
//key not found
return -1;
}
public int helper(Node head, int key) { // O(n)
if(head == null) {
return -1;
}
if(head.data == key) {
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) {
//calculate size
int sz = 0;
Node temp = head;
while(temp != null) {
temp = temp.next;
sz++;
}
if(n == sz) {
head = head.next; //removeFirst
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) { //helper
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 midNode
}
public boolean checkPalindrome() {
if(head == null || head.next == null) {
return true;
}
//step1 - find mid
Node midNode = findMid(head);
//step2 - 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;
//step3 - check left half & right half
while(right != null) {
if(left.data != right.data) {
return false;
}
left = left.next;
right = right.next;
}
return true;
}
public static boolean isCycle() {
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 exist
}
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 == 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; //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;
}
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 slow = head;
Node fast = head.next;
while(fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
Node mid = slow;
//reverse 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;
//alt merge - zig-zag merge
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.addLast(1);
ll.addLast(2);
ll.addLast(3);
ll.addLast(4);
ll.addLast(5);
ll.addLast(6);
//1->2->3->4->5
ll.print();
ll.zigZag();
ll.print();
}
}