public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode fast = head, slow = head;
while(fast != null){
if(fast.next == null || fast.next.next == null) return null;
fast = fast.next.next;
slow = slow.next;
if(fast == slow) break;
}
ListNode ans = head;
while(ans != slow){
ans = ans.next;
slow = slow.next;
}
return ans;
}
}
142. 环形链表 II
最新推荐文章于 2023-05-07 16:02:45 发布