-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedListQuestion.java
377 lines (311 loc) · 8.17 KB
/
LinkedListQuestion.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
375
376
/**
*
* @author pulkit4tech
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
class LinkedListQuestion implements Runnable {
BufferedReader c;
PrintWriter pout;
// static long mod = 1000000007;
public void run() {
try {
c = new BufferedReader(new InputStreamReader(System.in));
pout = new PrintWriter(System.out, true);
solve();
pout.close();
} catch (Exception e) {
pout.close();
e.printStackTrace();
System.exit(1);
}
}
public static void main(String[] args) throws Exception {
new Thread(new LinkedListQuestion()).start();
}
void solve() throws Exception {
// reverseLinkedList();
//detect_and_remove_loop();
double_merge_sort();
}
void double_merge_sort(){
dpush(15);
dpush(10);
dpush(5);
dpush(20);
dpush(2);
dpush(3);
dprintList(dhead);
dhead = mergeSortList(dhead);
dprintList(dhead);
}
DoubleListNode dhead;
class DoubleListNode {
int val;
DoubleListNode next;
DoubleListNode prev;
DoubleListNode(int x) {
val = x;
}
}
DoubleListNode mergeSortList(DoubleListNode head) {
if (head == null || head.next == null)
return head;
DoubleListNode second = split(head);
head = mergeSortList(head);
second = mergeSortList(second);
return merge(head,second);
}
DoubleListNode merge(DoubleListNode l, DoubleListNode r) {
if(l == null)
return r;
if(r == null)
return l;
if(l.val < r.val){
l.next = merge(l.next, r);
l.next.prev = l;
l.prev = null;
return l;
}else{
r.next = merge(l, r.next);
r.next.prev = r;
r.prev = null;
return r;
}
}
DoubleListNode split(DoubleListNode head){
DoubleListNode fast = head, slow = head;
while(fast.next != null && fast.next.next != null){
fast = fast.next.next;
slow = slow.next;
}
DoubleListNode temp = slow.next;
slow.next = null;
return temp;
}
void dpush(int data){
DoubleListNode new_node = new DoubleListNode(data);
if(dhead!=null)
dhead.prev = new_node;
new_node.next = dhead;
dhead = new_node;
}
void dprintList(DoubleListNode x) {
if(x != null){
DoubleListNode temp = null;
pout.println("forward:");
while (x != null) {
pout.print(x.val + " ");
temp = x;
x = x.next;
}
pout.println();
pout.println("backward:");
while(temp != null){
pout.print(temp.val + " ");
temp = temp.prev;
}
pout.println();
}
}
ListNode head;
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
ListNode mergeSortList(ListNode head) {
if (head == null || head.next == null)
return head;
// count total number of elements
int count = 0;
ListNode p = head;
while (p != null) {
count++;
p = p.next;
}
// break up to two list
int middle = count / 2;
ListNode l = head, r = null;
ListNode p2 = head;
int countHalf = 0;
while (p2 != null) {
countHalf++;
ListNode next = p2.next;
if (countHalf == middle) {
p2.next = null;
r = next;
}
p2 = next;
}
// now we have two parts l and r, recursively sort them
ListNode h1 = mergeSortList(l);
ListNode h2 = mergeSortList(r);
// merge together
ListNode merged = merge(h1, h2);
return merged;
}
ListNode merge(ListNode l, ListNode r) {
ListNode p1 = l;
ListNode p2 = r;
ListNode fakeHead = new ListNode(100);
ListNode pNew = fakeHead;
while (p1 != null || p2 != null) {
if (p1 == null) {
pNew.next = new ListNode(p2.val);
p2 = p2.next;
pNew = pNew.next;
} else if (p2 == null) {
pNew.next = new ListNode(p1.val);
p1 = p1.next;
pNew = pNew.next;
} else {
if (p1.val < p2.val) {
// if(fakeHead)
pNew.next = new ListNode(p1.val);
p1 = p1.next;
pNew = pNew.next;
} else if (p1.val == p2.val) {
pNew.next = new ListNode(p1.val);
pNew.next.next = new ListNode(p1.val);
pNew = pNew.next.next;
p1 = p1.next;
p2 = p2.next;
} else {
pNew.next = new ListNode(p2.val);
p2 = p2.next;
pNew = pNew.next;
}
}
}
// printList(fakeHead.next);
return fakeHead.next;
}
void push(int data){
ListNode new_node = new ListNode(data);
new_node.next = head;
head = new_node;
}
void printList(ListNode x) {
if(x != null){
pout.print(x.val + " ");
while (x.next != null) {
pout.print(x.next.val + " ");
x = x.next;
}
pout.println();
}
}
void reverseLinkedList() {
LinkedList<String> list = new LinkedList<>();
list.push("This");
list.push("is");
list.push("a");
list.push("test");
list.printList();
list.reverse(list.head, 4);
list.printList();
}
private void detect_and_remove_loop() {
LinkedList<Integer> list = new LinkedList();
list.push(50);
list.push(20);
list.push(15);
list.push(4);
list.push(10);
// Creating a loop for testing
list.head.next.next.next.next.next = list.head.next.next;
//list.printList(); it will enter into infinite loop .. comment it out to check
list.detectAndRemoveLoop(list.head);
System.out.println("Linked List after removing loop : ");
list.printList();
}
private class LinkedList<T> {
Node head;
class Node<T> {
T data;
Node next;
Node(T d) {
data = d;
next = null;
}
}
// Insert node at front
void push(T val) {
Node new_val = new Node(val);
new_val.next = head;
head = new_val;
}
// printing list
void printList() {
Node temp = head;
while (temp != null) {
pout.print(temp.data + " ");
temp = temp.next;
}
pout.println();
}
// reversing kth sub list
void reverse(Node head, int k) {
this.head = reverseHelper(head, k);
}
// helper function
Node reverseHelper(Node head, int k) {
Node current = head;
Node next = null;
Node prev = null;
int i = 0;
while (i < k && current != null) {
next = current.next;
current.next = prev;
prev = current;
current = next;
i++;
}
if (next != null) {
head.next = reverseHelper(next, k);
}
return prev;
}
// Function that detects loop in the list
boolean detectAndRemoveLoop(Node node) {
Node slow = node, fast = node;
while (slow != null && fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
// If slow and fast meet at same point then loop is present
if (slow == fast) {
removeLoop(slow, node);
return true;
}
}
return false;
}
// Function to remove loop
void removeLoop(Node loop, Node curr) {
Node ptr1 = curr, ptr2 = null;
while (true) {
/* Now start a pointer from loop_node and check if it ever
reaches ptr2 */
ptr2 = loop;
while (ptr2.next != loop && ptr2.next != ptr1) {
ptr2 = ptr2.next;
}
/* If ptr2 reahced ptr1 then there is a loop. So break the
loop */
if (ptr2.next == ptr1) {
break;
}
/* If ptr2 did't reach ptr1 then try the next node after ptr1 */
ptr1 = ptr1.next;
}
/* After the end of loop ptr2 is the last node of the loop. So
make next of ptr2 as NULL */
ptr2.next = null;
}
}
}