如果有环,最终肯定会在环上相遇。如果没环快指针先走完。
单链表形成环:尾针指向部位null
/**
* 快满指针判断链表是否有环
* @param head
* @return
*/
public static Boolean hasCycle(ListNode head){
if (null==head){
return false;
}
ListNode slow=head;
ListNode fast=head;
while (slow.next!=null&&fast.next!=null&&fast.next.next!=null){
slow=slow.next;
fast=fast.next.next;
if (slow==fast){
return true;
}
}
return false;
}