题目描述:
我的解答:
public class Solution {
public ListNode detectCycle(ListNode head) {
if(head==null||head.next==null){
return null;
}else {
ListNode slow=head;
ListNode fast=head;
do{
slow=slow.next;
fast=fast.next.next;
if(fast==null||fast.next==null){
return null;
}
}
while (slow!=fast);
ListNode index=head;
while (index!=slow){
index=index.next;
slow=slow.next;
}
return index;
}
}
}
写一下快慢指针相遇方程
得出在快慢指针相遇的时候有如上,也就是到环点的距离a等于c加某些圈。
注意我这里判断的时候是判断fast==slow这里用do while循环