forked from hariom20singh/foodewbpage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LL.java
343 lines (305 loc) · 7.94 KB
/
LL.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
public class LL {
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) {
size++;
Node newNode = new Node(data);
if(head == null) {
head = tail = newNode;
} else {
newNode.next = head;
head = newNode;
}
}
public void addLast(int data) {
size++;
Node newNode = new Node(data);
if(head == null) {
head = tail = newNode;
} else {
tail.next = newNode;
tail = newNode;
}
}
public void print() {
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;
}
size++;
Node temp = head;
int i = 0;
while(temp != null) {
if(i == idx-1) {//add here
Node newNode = new Node(data);
//insertion at middle
newNode.next = temp.next;
temp.next = newNode;
return;
}
temp = temp.next;
i++;
}
}
public int removeFirst() {
if(size == 0) {
System.out.println("empty LL");
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("empty LL");
return Integer.MIN_VALUE;
} else if(size == 1) {
int val = head.data;
head = tail = null;
size = 0;
return val;
}
Node temp = head;
for(int i=0; i<size-2; i++) {
temp = temp.next;
}
int val = temp.data;
temp.next = null;
tail = temp;
size--;
return val;
}
//Iterative
public int itSearch(int key) {
Node temp = head;
int i = 0;
while(temp != null) {
if(temp.data == key) {
return i;
}
temp = temp.next;
i++;
}
return -1;
}
public int recSearch(int key) {
return searchHelper(head, key);
}
public int searchHelper(Node head, int key) {
if(head == null) {
return -1;
}
if(head.data == key) {
return 0;
}
int idx = searchHelper(head.next, key);
if(idx == -1) {
return idx;
}
return idx+1;
}
public void reverse() {
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 removeNthfromEnd(int n) {
//size
int size = 0;
Node temp = head;
while(temp != null) {
temp = temp.next;
size++;
}
//if we have to remove the head
if(n == size) {
head = head.next;
return;
}
int i = 1;
int itf = size-n;
Node prev = head;
while(i < itf) { //try to find the node previous of nth
prev = prev.next;
i++;
}
prev.next = prev.next.next;
}
private Node findMidNode(Node head) {
Node slow = head;
Node fast = head.next;
//important because we want final mid to be end of 1st half in even case,
//not start of 2nd half. Because mid.next is start of 2nd half.
while(fast!= null && fast.next!=null) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
public boolean checkPalindrome() {
if(head == null || head.next == null) {
return true;
}
//find middle
Node mid = findMidNode(head);
//Reverse 2nd half
Node curr = mid;
Node prev = null;
while(curr != null) {
Node next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
Node right = prev;
Node left = head;
//check if equal
while(right != null) {
if(left.data != right.data) {
return false;
}
left = left.next;
right = right.next;
}
return true;
}
public boolean isCycle() {
Node slow = head;
Node fast = head;
while(fast!=null && fast.next!=null) {
fast = fast.next.next;
slow = slow.next;
if(fast == slow) {
return true;
}
}
return false;
}
public Node removeCycle() {
Node slow = head;
Node fast = head;
int flag = 0;
while(fast!=null && fast.next!=null) {
fast = fast.next.next;
slow = slow.next;
if(fast == slow) {
flag = 1;
break;
}
}
if(flag == 0)
return null;
slow = head;
int i = 0;
while(slow != fast) {
slow = slow.next;
fast = fast.next;
i++;
}
return slow;
}
private Node mergeSortHelper(Node head) {
if(head == null || head.next == null) {
return head;
}
Node mid = findMidNode(head);
//System.out.println(mid.data);
Node head2 = mid.next;
mid.next = null;
Node newHead1 = mergeSortHelper(head);
Node newHead2 = mergeSortHelper(head2);
// System.out.println(newHead1.data+" "+newHead2.data);
Node finalHead = merge(newHead1, newHead2);
return finalHead;
}
private Node merge(Node head1, Node head2) {
Node finalHead = new Node(-1);
Node temp = finalHead;
while(head1 != null && head2 != null) {
if(head1.data <= head2.data) {
temp.next = head1;
head1 = head1.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 finalHead.next;
}
public void mergeSort() {
head = mergeSortHelper(head);
}
public static void main(String args[]) {
LL ll = new LL();
//Add Nodes
ll.addLast(5);
ll.addLast(4);
ll.addLast(3);
ll.addLast(2);
//ll.addLast(1);
ll.print();
//System.out.println(ll.size);
ll.mergeSort();
ll.print();
/*
// Delete Nodes
ll.removeFirst();
ll.removeLast();
ll.removeFirst();
ll.removeLast();
ll.print();
System.out.println(ll.size);
// Iterative Search
System.out.println(ll.itSearch(1));
System.out.println(ll.itSearch(5));
// Recursive Search
System.out.println(ll.recSearch(1));
System.out.println(ll.recSearch(5));
//Reverse LL
ll.reverse();
ll.print();
*/
}
}