/**
* 使用快慢指针.如果有环,快指针一定会和慢指针重叠。
*/
public class Solution {
public boolean hasCycle(ListNode head) {
if(head == null){
return false;
}
ListNode slow=head;
ListNode fast=head.next;
while(fast!=null && fast.next!=null){
if(fast!=slow){
fast=fast.next.next;
slow=slow.next;
}else{
return true;
}
}
return false;
}
}