用HashSet存放以访问的节点,判断当前节点是否再HashSet中,用set.contains()判断
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public boolean hasCycle(ListNode head) {
Set<ListNode> set=new HashSet<>();
if(head==null){
return false;
}
while(head!=null){
if(set.contains(head)){
return true;
}
set.add(head);
head=head.next;
}
return false;
}
}