直接用set就行,如果set.add返回false,说明节点引用已经存在set中了!
public class Solution {
public boolean hasCycle(ListNode head) {
Set set = new HashSet<>();
while(head != null) {
if(set.add(head) == false) {
return true;
}
head = head.next;
}
return false;
}
}



