142. Linked List Cycle II

Posted apanda009

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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的主要内容,如果未能解决你的问题,请参考以下文章