2.6---找有环链表的开头结点(CC150)

 public ListNode detectCycle(ListNode head) {
        
    ListNode fast = head;
    ListNode slow = head;
    int flag = 0;
    ListNode intersection = null;
    while(fast != null && fast.next != null){
        fast = fast.next.next;
        slow = slow.next;
        if(fast == slow){
            intersection = fast;
            flag = 1;
            break;
        }
    }
    if(flag == 0 ) return null;
    slow = head;
    while(slow != fast){
        slow = slow.next;
        fast = fast.next;
        
    }
    return fast;

    }

 

posted @ 2015-12-18 09:18  创业-李春跃-增长黑客  阅读(161)  评论(0编辑  收藏  举报