[leetcode] Linked List Cycle II

来源:互联网 发布:获取ip及端口号 编辑:程序博客网 时间:2024/04/27 17:32
public ListNode detectCycle(ListNode head)
{
    ListNode slow, fast;
    slow = fast = head;
    while(slow != null && slow.next != null && fast != null && fast.next != null)
    {
        slow = slow.next;
        fast = fast.next.next;
        if(slow == fast)
        {
            ListNode p = head;
            while(p != slow)
            {
                p = p.next;
                slow = slow.next;
            }
            return p;
        }
    }
    return null;
}
0 0
原创粉丝点击