题目链接
思路
代码
class Solution {
public ListNode detectCycle(ListNode head) {
if(head == null){
return null;
}
ListNode slow = head;
ListNode fast = head;
do{
slow = slow.next;
fast = fast.next;
if(fast != null){
fast = fast.next;
}
}while(slow != null && fast != null && slow != fast);
if(fast == null){
return fast;
}
fast = head;
while(fast != slow){
fast = fast.next;
slow = slow.next;
}
return fast;
}
}
标签:II,head,slow,142,fast,next,链表,null
From: https://www.cnblogs.com/shixuanliu/p/17021645.html