Skip to content

Commit d37425b

Browse files
committed
Add LinkedListCycle.java
1 parent a6dc8d3 commit d37425b

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

LinkedListCycle.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
*
3+
*/
4+
package leetcode;
5+
6+
import java.util.HashSet;
7+
import java.util.Set;
8+
9+
/**
10+
* @author hzlinqien
11+
*
12+
*/
13+
14+
public class LinkedListCycle {
15+
16+
public boolean hasCycle(ListNode head) {
17+
Set<ListNode> nodeMap = new HashSet<>();
18+
for (ListNode temp = head; temp != null; temp = temp.next) {
19+
if (nodeMap.contains(temp)) {
20+
return true;
21+
}
22+
nodeMap.add(temp);
23+
}
24+
return false;
25+
}
26+
27+
class ListNode {
28+
int val;
29+
ListNode next;
30+
31+
ListNode(int x) {
32+
val = x;
33+
next = null;
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)