题目:
代码:
public class Solution {
public boolean hasCycle(ListNode head) {
//哈希值集合
Set<ListNode> set=new HashSet<>();
while(head!=null){
if(set.contains(head)){
return head;
}else{
set.add(head);
head=head.next;
}
}
return null;
}
}
运行结果: