142. Linked List Cycle II

public class Solution {
    public ListNode detectCycle(ListNode head) {
        ListNode p=head,q=head;
        while(p!=null&&p.next!=null)
        {
            p=p.next.next;
            q=q.next;
            if(p==q)
                break;
        }
        if(p==null||p.next==null)
            return null;
        p=head;
        while(p!=q)
        {
            p=p.next;
            q=q.next;
        }
        return p;
    }
}

  

原文地址:https://www.cnblogs.com/asuran/p/7644992.html