forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDetectLoop.java
78 lines (72 loc) · 2.09 KB
/
DetectLoop.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
package me.ramswaroop.linkedlists;
import me.ramswaroop.common.SingleLinkedList;
import me.ramswaroop.common.SingleLinkedNode;
import java.util.HashMap;
/**
* Created by IntelliJ IDEA.
*
* @author: ramswaroop
* @date: 6/19/15
* @time: 9:24 AM
*/
public class DetectLoop {
/**
* Uses Flyod's Cycle Finding algorithm.
* <p/>
* This is the fastest method. Traverse
* linked list using two pointers. Move one
* pointer by one and other pointer by two.
* If these pointers meet at some node then
* there is a loop. If pointers do not meet
* then linked list does not have loop.
* <p/>
* Time: O(n)
* Space: O(1)
*
* @param list
* @return
*/
public static <E extends Comparable<E>> boolean isLoopPresent(SingleLinkedList<E> list) {
SingleLinkedNode<E> slow = list.head, fast = slow.next;
while (fast != null && fast.next != null) {
if (slow == fast) {
return true;
}
slow = slow.next;
fast = fast.next.next;
}
return false;
}
/**
* Uses HashMap to store visited nodes.
* <p/>
* Time: O(n)
* Space: O(n)
*
* @param node
* @return
*/
public static <E extends Comparable<E>> boolean isLoopPresentUsingHashMap(SingleLinkedNode<E> node) {
HashMap<SingleLinkedNode<E>, Boolean> map = new HashMap<>();
SingleLinkedNode<E> curr = node;
while (curr != null) {
if (map.get(curr) != null && map.get(curr) == true) {
return true;
}
map.put(curr, true);
curr = curr.next;
}
return false;
}
public static void main(String a[]) {
SingleLinkedList<Integer> linkedList = new SingleLinkedList<>();
linkedList.add(00);
linkedList.add(11);
linkedList.add(22);
linkedList.add(33);
linkedList.add(44);
linkedList.add(55);
linkedList.getNode(4).next = linkedList.getNode(3);
System.out.println(isLoopPresent(linkedList));
}
}