-
Notifications
You must be signed in to change notification settings - Fork 0
/
HasCycle.java
132 lines (109 loc) · 2.66 KB
/
HasCycle.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
package com.rrohit.algo.linkedlist;
import java.util.HashMap;
import java.util.Map;
public class HasCycle {
public <T> Node<T> hashCycleUsingMap(Node<T> head) {
if (head == null) {
return null;
}
Map<Node<T>, Node<T>> map = new HashMap<Node<T>, Node<T>>();
Node<T> current = head;
while(current.getNext() != null) {
if (map.containsKey(current)) {
return current;
}
map.put(current, current.getNext());
current = current.getNext();
}
System.out.println("No Loop : Null Terminated List");
return null;
}
public <T> Node<T> hashCycle(Node<T> head) {
if (head == null) {
return null;
}
Node<T> slow = head, fast = head;
while (fast != null) {
fast = fast.getNext();
if (fast == head) {
System.out.println("Head : Loop Found");
return fast;
}
if (fast == null) {
System.out.println("No Loop: Null Terminated");
return null;
}
if (slow == fast) {
System.out.println("Loop Found :");
return slow;
}
fast = fast.getNext();
if (slow == fast) {
System.out.println("Loop Found :");
return slow;
}
slow = slow.getNext();
}
return null;
}
public int findSize(Node head) {
Node current = head;
int size=1;
while (current.getNext() != null) {
size++;
current = current.getNext();
}
return size;
}
public <T> Node<T> findLoopNode(Node<T> head){
Node<T> loopNode = hashCycle(head);
if (loopNode == null) {
System.out.println("Null Terminated Linked List");
return null;
}
/*
* Loop found, now break the list and find out the intersection point.
*/
Node<T> head1 = loopNode.getNext();
loopNode.setNext(null);
Node<T> current = head, current1= head1;
int size = findSize(head); // list size
int size1 = findSize(head1); // list1 size
System.out.println("Size of loop = "+size1);
int diff = 0;
if (size > size1) {
diff = size-size1;
while (diff>0){
current = current.getNext();
diff--;
}
}else{
diff = size1-size;
while (diff>0){
current1 = current1.getNext();
diff--;
}
}
while (current != current1) {
current = current.getNext();
current1 = current1.getNext();
}
System.out.println("Loop Node ::"+current);
return current;
}
public static void main(String[] args) {
LinkedList<Integer> list = new LinkedList<Integer>();
Node<Integer> loopNode = null;
for (int i=1; i<10; i++) {
Node<Integer> node = new Node<Integer>(i);
list.add(node);
if (i==1){
loopNode = node;
}
}
list.getTail().setNext(loopNode);
//list.display();
HasCycle hc = new HasCycle();
System.out.println("Loop Node : "+hc.findLoopNode(list.getHead()));
}
}