public boolean hasCycle(Node head) {
if(head==null||head.next==null||head.next.next==null) return false;
Node fast = head.next.next;
Node slow = head.next;
while(fast!=slow){
if(fast.next==null||fast.next.next==null) return false;
fast = fast.next.next;
slow = slow.next;
}
return true;
}
网友评论