public class Solution {
public ListNode detectCycle(ListNode head) {
if(null == head)
return null;
ListNode fast = head, slow = head;
while (true) {
if (fast == null || fast.next == null) return null;
fast = fast.next.next;
slow = slow.next;
if (fast == slow) break;
}
fast = head;
while (slow != fast) {
slow = slow.next;
fast = fast.next;
}
return fast;
}
}
LeetCode 142环形链表2(java)
最新推荐文章于 2024-08-04 15:52:01 发布