public boolean hasCycle(ListNode head) {
if (head == null || head.next == null)return false;
ListNode lowNode = head;//使用快慢指针的方法
ListNode fastNode = head.next.next;
while(fastNode != null && fastNode.next != null){
if (lowNode == fastNode){//如果有环,慢指针就会就会追上快指针
return true;
}else{
lowNode = lowNode.next;
fastNode = fastNode.next.next;
}
}
return false;
}
https://leetcode-cn.com/problems/linked-list-cycle/submissions/