forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDetectAndRemoveLoop.java
105 lines (94 loc) · 3.91 KB
/
DetectAndRemoveLoop.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
package com.rampatra.linkedlists;
import com.rampatra.base.SingleLinkedList;
import com.rampatra.base.SingleLinkedNode;
/**
* See this <a href="https://stackoverflow.com/a/32190575/1385441">Stackoverflow post</a> to understand
* how to find the starting node of the loop.
* <p>
* Proof for Flyod's Loop Detection Algorithm:
* <p>
* Suppose fastRunner had just skipped over slowRunner. fastRunner would only be 1 node ahead of slowRunner, since their
* speeds differ by only 1. So we would have something like this:
* <p>
* [ ] -> [s] -> [f]
* <p>
* What would the step right before this "skipping step" look like? fastRunner would be 2 nodes back, and slowRunner
* would be 1 node back. But wait, that means they would be at the same node! So fastRunner didn't skip over slowRunner!
* (This is a proof by contradiction.)
*
* @author rampatra
* @since 7/1/15
*/
public class DetectAndRemoveLoop {
/**
* Detects loop, if any, in {@code list} and removes it.
* <p>
* Approach:
* 1) Use Floyd's cycle detection algorithm to detect loop.
* 2) Acc. to FCD, once the fast pointer meets the slow pointer we conclude that there is a loop.
* 4) Now that we have concluded there is a loop, let's detect the starting node and remove the loop:
* i. Move the slow pointer to head.
* ii. Now, move both slow and fast pointer at same pace and where they meet is the starting point of the loop.
* iii. Lastly, to remove the loop make the next of the node (before the starting point of loop) to null.
*
* Proof for Floyd's Cycle Detection: Consider a cyclic list and imagine the slow and fast pointers are two runners
* racing around a circle track. The fast runner will eventually meet the slow runner. Why? Consider this case -
* The fast runner is just one step behind the slow runner. In the next iteration, they both increment one and two
* steps respectively and meet each other.
*
* @param list
* @param <E>
* @return {@code true} if loop exists {@code false} otherwise.
*/
public static <E extends Comparable<E>> boolean detectAndRemoveLoop(SingleLinkedList<E> list) {
boolean isLoopPresent = false;
SingleLinkedNode<E> slow = list.head, fast = list.head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) {
isLoopPresent = true;
break;
}
}
if (!isLoopPresent) return false;
slow = list.head;
// move both pointers at same pace to determine the starting node of loop
while (true) {
slow = slow.next;
fast = fast.next;
if (slow == fast) {
fast.next = null;
break;
}
}
return isLoopPresent;
}
public static void main(String[] args) {
SingleLinkedList<Integer> linkedList = new SingleLinkedList<>();
linkedList.add(0);
linkedList.add(1);
linkedList.add(2);
linkedList.add(3);
linkedList.add(4);
linkedList.add(5);
linkedList.getNode(4).next = linkedList.getNode(2);
System.out.println(detectAndRemoveLoop(linkedList));
linkedList.printList();
linkedList = new SingleLinkedList<>();
linkedList.add(0);
linkedList.add(1);
linkedList.getNode(1).next = linkedList.getNode(0);
System.out.println(detectAndRemoveLoop(linkedList));
linkedList.printList();
linkedList = new SingleLinkedList<>();
linkedList.add(0);
System.out.println(detectAndRemoveLoop(linkedList));
linkedList.printList();
linkedList = new SingleLinkedList<>();
linkedList.add(0);
linkedList.getNode(0).next = linkedList.getNode(0);
System.out.println(detectAndRemoveLoop(linkedList));
linkedList.printList();
}
}