-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedLists_Operations.java
374 lines (293 loc) · 7.89 KB
/
LinkedLists_Operations.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
public class LinkedLists_Operations {
public static Node head;
public static Node tail;
public static int size;
public static class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
public static void add_first(int data) {
Node new_node = new Node(data);
size++;
if (head == null){
head = new_node;
tail = new_node;
return;
}
new_node.next = head;
head = new_node;
}
public static void add_last(int data) {
Node new_node = new Node(data);
size++;
if (head == null){
head = new_node;
tail = new_node;
return;
}
tail.next = new_node;
tail = new_node;
}
public static void print() {
Node temp = head;
if (head==null){
System.out.print("Empty list");
return;
}
while (temp != null){
System.out.print(temp.data + " --> ");
temp = temp.next;
}
System.out.print("null");
System.out.println();
}
public static void add_middle(int index, int data){
if (head==null){
add_first(data);
return;
}
Node new_node = new Node(data);
size++;
Node temp = head;
int i = 0;
while (i < index-1){
temp = temp.next;
i++;
}
new_node.next = temp.next;
temp.next = new_node;
}
public static void remove_first(){
if (size == 0) {
System.out.println("Empty List");
return;
}
if (size==1){
head = null;
tail = null;
size = 0;
return;
}
head = head.next;
size--;
}
public static int remove_last(){
if (size == 0) {
System.out.println("Empty list");
return Integer.MIN_VALUE;
}
if (size == 1){
size = 0;
head = null;
tail = null;
int val = head.data;
return val;
}
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 static void remove_index(int index){
if (size==0) {
System.out.println("Empty list");
return;
}
if (size == 1) {
head = null;
tail = null;
size = 0;
return;
}
Node temp = head;
Node ptr = temp.next;
int i = 0;
while (i < index-1){
temp = ptr;
ptr = ptr.next;
i++;
}
temp.next = ptr.next;
size--;
}
public static int itr_search(int key){
Node temp = head;
int i = 0;
while (temp != null){
if (temp.data == key){
return i;
}
i++;
temp = temp.next;
}
return -1;
}
public static int helper(int key, Node head){
if (head == null) return -1;
if (head.data == key) return 0;
int index = helper(key, head.next);
if (index == -1) return -1;
return index+1;
}
public static int rec_search(int key){
return helper(key, head);
}
public static void reverse(){
Node prev = null;
Node curr = head;
Node next;
while (curr != null){
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
head = prev;
}
public static void remove_from_last(int n){
// calculating size
int size = 0;
Node temp = head;
while (temp!=null){
temp = temp.next;
size++;
}
// if the last node to first is head
if (n==size){
head = head.next;
return;
}
int index = size-n;
int i=0;
Node prev = head;
while (i<index){
prev = prev.next;
i++;
}
prev.next = prev.next.next;
return;
}
// LL is Palindrome
public static Node find_mid(Node head){
Node slow = head;
Node fast = head;
while (fast != null && fast.next != null){
slow = slow.next;
fast = fast.next.next;
}
Node mid = slow;
return mid;
}
public static boolean palindrome(){
// base case
if (head == null || head.next == null) return true;
// find the mid
Node mid = find_mid(head);
// Reverse the second half
Node prev = null;
Node curr = mid;
Node next;
while (curr != null){
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
Node left = head;
Node right = prev;
while (right != null){
if (left.data != right.data) return false;
left = left.next;
right = right.next;
}
return true;
}
public static void zig_zag(){
if (head == null || head.next == null){
return;
}
// 1. find mid node
Node slow = head;
Node fast = head.next;
while (fast != null && fast.next != null){
slow = slow.next;
fast = fast.next.next;
}
Node midNode = slow;
// 2. reverse second half
Node curr = midNode.next;
midNode.next = null;
Node prev = null;
Node next;
while (curr!=null){
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
Node left_head = head;
Node right_head = prev;
// 3. alternate merging
Node nextL;
Node nextR;
while (left_head != null && right_head != null){
nextL = left_head.next; // store in nextL
left_head.next = right_head;
nextR = right_head.next; // store in nextR
right_head.next = nextL;
// update
left_head = nextL;
right_head = nextR;
}
}
public static void main(String[] args) {
LinkedLists_Operations l1 = new LinkedLists_Operations();
l1.add_first(2);
l1.add_first(10);
l1.add_first(1);
l1.add_middle(1, 4);
l1.add_middle(2, 6);
l1.add_last(11);
l1.add_last(3);
l1.add_last(5);
l1.print();
System.out.println(l1.size);
l1.remove_first();
l1.remove_last();
l1.print();
l1.remove_index(2);
l1.print();
int ind = l1.itr_search(10);
System.out.println(ind);
int rec_ind = l1.rec_search(2);
System.out.println(rec_ind);
l1.add_first(3);
l1.add_first(2);
l1.add_first(1);
l1.add_last(5);
l1.remove_last();
l1.remove_last();
l1.remove_last();
l1.remove_last();
l1.remove_last();
l1.add_last(5);
l1.add_last(6);
l1.print();
// REVERSE LL
// l1.reverse();
// l1.print();
// l1.remove_from_last(3);
// System.out.println(l1.palindrome());
l1.zig_zag();
l1.print();
}
}