首页 > 代码库 > 142. Linked List Cycle II

142. Linked List Cycle II

https://leetcode.com/problems/linked-list-cycle-ii/#/description

 

public ListNode detectCycle(ListNode head) {
      
        if (head == null) {
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        do {
            if (fast == null || fast.next == null) {
                return null;
            }
            fast = fast.next.next;
            slow = slow.next;
        } while (fast != slow);
        fast = head;
        while (fast != slow) {
            fast = fast.next;
            slow = slow.next;
        }
        return fast;
    }

  

142. Linked List Cycle II