题目:
想法:
利用快慢指针,快走两步,慢走一步。如果存在环,则快慢会相遇)
代码:
public class Solution {
public boolean hasCycle(ListNode head) {
if(head==null||head.next==null) return false;
ListNode fast=head.next,slow=head;
while(fast!=slow){
if(fast==null||fast.next==null) return false;
fast=fast.next.next;
slow=slow.next;
}
return true;
}
}