-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCycleLinkedList.java
More file actions
57 lines (47 loc) · 1.21 KB
/
CycleLinkedList.java
File metadata and controls
57 lines (47 loc) · 1.21 KB
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
/**
*
*/
package a2;
import java.util.*;
import static org.junit.Assert.*;
public class CycleLinkedList {
// bool to check Node, returns true if a cycle exists
public static boolean hasCycle(Node head) {
// creates stack
Stack<Node> nodeStack = new Stack<Node>();
// while loop continues till head is null
while (head != null) {
// if checks if nodeStack contains head Node value and returns true
if (nodeStack.contains(head)) {
return true;
}
// adds head to nodeStack and sets head as next value in the list.
nodeStack.add(head);
head = head.getNext();
}
// returns false when if in while is not satisfied
return false;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Node head = new Node("start");
Node prev = head;
for (int i = 0; i < 4; i++) {
Node temp = new Node(Integer.toString(i));
prev.setNext(temp);
prev = temp;
}
boolean b = hasCycle(head);
System.out.println("Testing...");
assertEquals(b, false);
System.out.println("Success!");
prev.setNext(head.getNext());
b = hasCycle(head);
System.out.println("Testing...");
assertEquals(b, true);
System.out.println("Success!");
}
}