@@ -44,16 +44,14 @@ public SinglyLinkedList(Node head, int size) {
44
44
public boolean detectLoop() {
45
45
Node currentNodeFast = head;
46
46
Node currentNodeSlow = head;
47
- boolean flag = false;
48
- while (currentNodeFast != null && currentNodeFast.next != null && currentNodeSlow != null && currentNodeSlow.next != null) {
47
+ while (currentNodeFast != null && currentNodeFast.next != null) {
49
48
currentNodeFast = currentNodeFast.next.next;
50
49
currentNodeSlow = currentNodeSlow.next;
51
50
if (currentNodeFast == currentNodeSlow) {
52
- flag = true;
53
- break;
51
+ return true;
54
52
}
55
53
}
56
- return flag ;
54
+ return false ;
57
55
}
58
56
59
57
/**
@@ -84,7 +82,7 @@ public void swapNodes(int valueFirst, int valueSecond) {
84
82
if(previousA != null){
85
83
previousA.next = currentB;
86
84
}
87
- else{
85
+ else{
88
86
// make 'b' as the new head
89
87
head = currentB;
90
88
}
@@ -98,7 +96,7 @@ public void swapNodes(int valueFirst, int valueSecond) {
98
96
head = currentA;
99
97
}
100
98
// Swap next pointer
101
-
99
+
102
100
Node temp = currentA.next;
103
101
currentA.next = currentB.next;
104
102
currentB.next = temp;
@@ -109,15 +107,19 @@ public void swapNodes(int valueFirst, int valueSecond) {
109
107
*
110
108
*/
111
109
Node reverseList(Node node) {
110
+ Node prevNode = head;
111
+ while(prevNode.next!=node){
112
+ prevNode = prevNode.next;
113
+ }
112
114
Node prev = null, curr = node, next;
113
115
while (curr != null) {
114
116
next = curr.next;
115
117
curr.next = prev;
116
118
prev = curr;
117
119
curr = next;
118
120
}
119
- node = prev;
120
- return node ;
121
+ prevNode.next = prev;
122
+ return head ;
121
123
}
122
124
123
125
/**
@@ -161,6 +163,14 @@ public Node getHead() {
161
163
return head;
162
164
}
163
165
166
+ /**
167
+ * Set head of the list.
168
+ *
169
+ */
170
+ public void setHead(Node head) {
171
+ this.head = head;
172
+ }
173
+
164
174
/**
165
175
* Calculate the count of the list manually
166
176
*
@@ -205,145 +215,41 @@ public String toString() {
205
215
return joiner.toString();
206
216
}
207
217
208
- /**
209
- * Driver Code
210
- */
211
- public static void main(String[] arg) {
212
- SinglyLinkedList list = new SinglyLinkedList();
213
- assert list.isEmpty();
214
- assert list.size() == 0 && list.count() == 0;
215
- assert list.toString().equals("");
216
-
217
- /* Test insert function */
218
- list.insertHead(5);
219
- list.insertHead(7);
220
- list.insertHead(10);
221
- list.insert(3);
222
- list.insertNth(1, 4);
223
- assert list.toString().equals("10->7->5->3->1");
224
-
225
- /* Test search function */
226
- assert list.search(10) && list.search(5) && list.search(1) && !list.search(100);
227
-
228
- /* Test get function */
229
- assert list.getNth(0) == 10 && list.getNth(2) == 5 && list.getNth(4) == 1;
230
-
231
- /* Test delete function */
232
- list.deleteHead();
233
- list.deleteNth(1);
234
- list.delete();
235
- assert list.toString().equals("7->3");
236
-
237
- assert list.size == 2 && list.size() == list.count();
238
-
239
- list.clear();
240
- assert list.isEmpty();
241
-
242
- try {
243
- list.delete();
244
- assert false;
245
- /* this should not happen */
246
- } catch (Exception e) {
247
- assert true;
248
- /* this should happen */
249
- }
250
-
251
- Node instance = new Node();
252
- Node head = new Node(0, new Node(2, new Node(3, new Node(3, new Node(4)))));
253
- head = instance.deleteDuplicates(head);
254
- instance.print(head);
255
-
256
- }
257
- }
258
-
259
- /**
260
- * This class is the nodes of the SinglyLinked List. They consist of a value and
261
- * a pointer to the node after them.
262
- */
263
- class Node {
264
-
265
- /**
266
- * Head refer to the front of the list
267
- */
268
- public Node head;
269
-
270
- /**
271
- * Size of SinglyLinkedList
272
- */
273
- public int size;
274
-
275
-
276
-
277
- /**
278
- * The value of the node
279
- */
280
- int value;
281
-
282
- /**
283
- * Point to the next node
284
- */
285
- Node next;
286
-
287
- Node() {
288
- }
289
-
290
- /**
291
- * Constructor
292
- *
293
- * @param value Value to be put in the node
294
- */
295
- Node(int value) {
296
- this(value, null);
297
- }
218
+ public void deleteDuplicates() {
298
219
299
- /**
300
- * Constructor
301
- *
302
- * @param value Value to be put in the node
303
- * @param next Reference to the next node
304
- */
305
- Node(int value, Node next) {
306
- this.value = value;
307
- this.next = next;
308
- }
309
-
310
- public Node deleteDuplicates(Node head) {
311
- // sentinel
312
- Node sentinel = new Node(0, head);
313
-
314
- // predecessor = the last node
315
- // before the sublist of duplicates
316
- Node pred = sentinel;
317
-
318
- while (head != null) {
220
+ Node pred = head;
221
+ // predecessor = the node
222
+ // having sublist of its duplicates
223
+ Node newHead = head;
224
+ while (newHead != null) {
319
225
// if it's a beginning of duplicates sublist
320
226
// skip all duplicates
321
- if (head .next != null && head .value == head .next.value) {
227
+ if (newHead .next != null && newHead .value == newHead .next.value) {
322
228
// move till the end of duplicates sublist
323
- while (head .next != null && head .value == head .next.value) {
324
- head = head .next;
229
+ while (newHead .next != null && newHead .value == newHead .next.value) {
230
+ newHead = newHead .next;
325
231
}
326
232
// skip all duplicates
327
- pred.next = head.next;
233
+ pred.next = newHead.next;
234
+ newHead = null;
235
+
328
236
// otherwise, move predecessor
329
- } else {
330
- pred = pred.next;
331
237
}
332
-
333
238
// move forward
334
- head = head.next;
239
+ pred = pred.next;
240
+ newHead = pred;
335
241
}
336
- return sentinel.next;
337
242
}
338
243
339
- public void print(Node head ) {
244
+ public void print() {
340
245
Node temp = head;
341
246
while (temp != null && temp.next != null) {
342
247
System.out.print(temp.value + "->");
343
248
temp = temp.next;
344
249
}
345
250
if (temp != null) {
346
251
System.out.print(temp.value);
252
+ System.out.println();
347
253
}
348
254
}
349
255
@@ -379,39 +285,29 @@ public void insertNth(int data, int position) {
379
285
head = newNode;
380
286
size++;
381
287
return;
382
- } else if (position == 0) {
288
+ }
289
+ if (position == 0) {
383
290
/* insert at the head of the list */
384
291
newNode.next = head;
385
292
head = newNode;
386
293
size++;
387
294
return;
388
295
}
296
+
389
297
Node cur = head;
390
298
for (int i = 0; i < position - 1; ++i) {
391
299
cur = cur.next;
392
300
}
393
301
newNode.next = cur.next;
394
302
cur.next = newNode;
395
303
size++;
304
+
396
305
}
397
306
398
307
/**
399
308
* Swaps nodes of two given values a and b.
400
309
*
401
310
*/
402
- public void swapNodes(int a, int b) {
403
- Node currentNode = head;
404
- Node temp = null;
405
- while (currentNode != null) {
406
- if (currentNode.next.value == a) {
407
- temp = currentNode.next;
408
- }
409
- if (currentNode.next.value == b) {
410
- currentNode.next = temp;
411
- }
412
- currentNode = currentNode.next;
413
- }
414
- }
415
311
416
312
/**
417
313
* Deletes a node at the head
@@ -479,4 +375,96 @@ public void checkBounds(int position, int low, int high) {
479
375
throw new IndexOutOfBoundsException(position + "");
480
376
}
481
377
}
378
+ /**
379
+ * Driver Code
380
+ */
381
+ public static void main(String[] arg) {
382
+ SinglyLinkedList list = new SinglyLinkedList();
383
+ assert list.isEmpty();
384
+ assert list.size() == 0 && list.count() == 0;
385
+ assert list.toString().equals("");
386
+
387
+ /* Test insert function */
388
+ list.insertHead(5);
389
+ list.insertHead(7);
390
+ list.insertHead(10);
391
+ list.insert(3);
392
+ list.insertNth(1, 4);
393
+ assert list.toString().equals("10->7->5->3->1");
394
+ System.out.println(list.toString());
395
+ /* Test search function */
396
+ assert list.search(10) && list.search(5) && list.search(1) && !list.search(100);
397
+
398
+ /* Test get function */
399
+ assert list.getNth(0) == 10 && list.getNth(2) == 5 && list.getNth(4) == 1;
400
+
401
+ /* Test delete function */
402
+ list.deleteHead();
403
+ list.deleteNth(1);
404
+ list.delete();
405
+ assert list.toString().equals("7->3");
406
+ System.out.println(list.toString());
407
+ assert list.size == 2 && list.size() == list.count();
408
+
409
+ list.clear();
410
+ assert list.isEmpty();
411
+
412
+ try {
413
+ list.delete();
414
+ assert false;
415
+ /* this should not happen */
416
+ } catch (Exception e) {
417
+ assert true;
418
+ /* this should happen */
419
+ }
420
+
421
+ SinglyLinkedList instance = new SinglyLinkedList();
422
+ Node head = new Node(0, new Node(2, new Node(3, new Node(3, new Node(4)))));
423
+ instance.setHead(head);
424
+ instance.deleteDuplicates();
425
+ instance.print();
426
+
427
+ }
428
+
429
+ }
430
+
431
+ /**
432
+ * This class is the nodes of the SinglyLinked List. They consist of a value and
433
+ * a pointer to the node after them.
434
+ */
435
+ class Node {
436
+
437
+ /**
438
+ * The value of the node
439
+ */
440
+ int value;
441
+
442
+ /**
443
+ * Point to the next node
444
+ */
445
+ Node next;
446
+
447
+ Node() {
448
+ }
449
+
450
+ /**
451
+ * Constructor
452
+ *
453
+ * @param value Value to be put in the node
454
+ */
455
+ Node(int value) {
456
+ this(value, null);
457
+ }
458
+
459
+ /**
460
+ * Constructor
461
+ *
462
+ * @param value Value to be put in the node
463
+ * @param next Reference to the next node
464
+ */
465
+ Node(int value, Node next) {
466
+ this.value = value;
467
+ this.next = next;
468
+ }
469
+
482
470
}
0 commit comments