-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedlist.java
More file actions
367 lines (329 loc) · 8.82 KB
/
Linkedlist.java
File metadata and controls
367 lines (329 loc) · 8.82 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
public class Linkedlist {
// Inner Node class
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;
// we will build methods to add , remove , print and other operations on LL's
public void addFirst(int data) { // O(k) constant time complexity
// add new node
Node newNode = new Node(data);
size++;
if (head == null) {
head = tail = newNode;
return;
}
// new node ka pointer head ko point karega
newNode.next = head;
// newNode ko head bana denge
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 int removeFirst() {
if (size == 0) {
System.out.println("LL is empty");
return -1;
} 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");
} 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++) {
prev = prev.next;
}
int val = tail.data;
prev.next = null;
tail = prev;
size--;
return val;
}
public int helper(Node head, int target) { // tc and sc both O(n)
if (head == null) {
return -1;
}
if (head.data == target) {
return 0;
}
int idx = helper(head.next, target);
if (idx == -1) {
return -1;
}
return idx + 1; // cannot use idx++ as it increases the value before returning so the actual
// value is never return
}
public int recSearch(int target) {
return helper(head, target);
}
public static int itrSearch(int target) {
Node temp = head;
int i = 0;
while (temp != null) {
if (temp.data == target) {
return i;
}
temp = temp.next;
i++;
}
return -1;
}
public void reverseLL() { // 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 removeNthFromEnd(int n) {
int sz = 0;
Node temp = head;
while (temp != null) {
temp = temp.next;
sz++;
}
if (n == sz) {
head = head.next; // if we are asked to remove nth from last ans n = size -->head
}
int i = 1;
int iTofind = sz - n;
Node prev = head;
while (i < iTofind) {
prev = prev.next;
i++;
}
prev.next = prev.next.next;
return;
}
public Node findMid(Node head) {
Node slow = head;
Node fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
public boolean checkPalindromeLL() {
if (head == null || head.next == null) {
return true;
}
Node midNode = findMid(head);
Node prev = null;
Node curr = midNode;
Node next;
while (curr != null) {
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
Node right = prev;
Node left = head;
while (right != null) {
if (left.data != right.data) {
return false;
}
left = left.next;
right = right.next;
}
return true;
}
public void print() { // O(n)
Node temp = head;
while (temp != null) {
System.out.print(temp.data + " ");
temp = temp.next;
}
System.out.println();
}
public void addMiddle(int idx, int data) {
Node newNode = new Node(data);
size++;
Node temp = head;
int i = 0;
if (idx == 0) {
addFirst(data);
return;
}
while (i < idx - 1) {
temp = temp.next;
i++;
}
newNode.next = temp.next;
temp.next = newNode;
}
public boolean isCycle() {
Node slow = head;
Node fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) {
return true;
}
}
return false;
}
public void removeCycle() {
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;
}
slow = head;
Node prev = null;
while (slow != fast) {
prev = fast;
slow = slow.next;
fast = fast.next;
}
prev.next = null;
}
public 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;
}
public Node merge(Node head1, Node head2) {
Node mergedLL = new Node(-1); // dummy node
Node temp = mergedLL;
// Merge until one list ends
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;
}
// Attach remaining nodes (if any)
if (head1 != null) {
temp.next = head1;
} else {
temp.next = head2;
}
return mergedLL.next;
}
public Node mergeSort(Node head) {
if (head == null || head.next == null) {
return head;
}
Node mid = getMid(head);
Node rightHead = mid.next;
mid.next = null;
Node newLeft = mergeSort(head);
Node newRight = mergeSort(rightHead);
return merge(newLeft, newRight);
}
public void ZigZagLL() {
Node slow = head;
Node fast = head;
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;
// 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.addFirst(10);
// LL.addFirst(20);
// LL.addFirst(30);
// LL.addLast(35);
// LL.addLast(40);
// LL.addMiddle(2, 60);
// // LL.removeFirst();
// // LL.removeLast();
// LL.print();
// // System.out.println(LL.recSearch(35));
// // System.out.println("size " + size);
// LL.reverseLL();
// LL.print();
// LL.removeNthFromEnd(3);
LL.head = LL.new Node(1);
LL.head.next = LL.new Node(2);
LL.head.next.next = LL.new Node(3);
LL.head.next.next.next = LL.new Node(4);
LL.head.next.next.next.next = LL.new Node(5);
// LL.removeCycle();
LL.print();
// LL.mergeSort(LL.head);
LL.ZigZagLL();
LL.print();
// System.out.println(LL.isCycle());
}
}