Skip to content

Commit 0f48677

Browse files
committed
Time: 9 ms (13.40%), Space: 45.3 MB (37.58%) - LeetHub
1 parent 6b5435f commit 0f48677

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) {
7+
* val = x;
8+
* next = null;
9+
* }
10+
* }
11+
*/
12+
public class Solution {
13+
public ListNode detectCycle(ListNode head) {
14+
HashSet<ListNode> nodesSet = new HashSet<>();
15+
16+
while (head != null) {
17+
{
18+
if (nodesSet.contains(head))
19+
return head;
20+
21+
nodesSet.add(head);
22+
head = head.next;
23+
}
24+
}
25+
return null;
26+
}
27+
}

0 commit comments

Comments
 (0)