public static boolean hasCycle(Node head){
if(head==null||head.next==null){
return false;
}
Node slow=head;
Node fast=head.next;
while(slow!=head){
if(fast==null&&fast.next==null){
//表明没有环
return false;
}
slow=slow.next;
fast=fast.next;
}
return true;
}
public static Node detectCycle(Node head){
if(head==null||head.next==null){
return null;
}
Node slow=head;
Node fast=head.next;
while(slow!=head){
if(fast==null&&fast.next==null){
return null;
}
slow=slow.next;
fast=fast.next;
}
fast=head;
while(fast!=slow){
slow=slow.next;
fast=fast.next;
}
return slow;
}