141.Linked List Cycle (E)
https://leetcode.com/problems/linked-list-cycle/
1.Description(Medium)
2.Code
public boolean hasCycle(ListNode head) {
if(head==null ||head.next==null){
return false;
}
ListNode slow=head;
ListNode fast=head.next;//注意fast是head.next
while(fast!=slow){
//fast可能先结束
if(fast==null ||fast.next==null){
return false;
}
fast=fast.next.next;
slow=slow.next;
}
return true;
}Last updated