[力扣Hot 100------第1題--142.環形鏈表II]

注意考慮下特殊情況,即什麼時候返回null(鏈表長度爲0或1或壓根無環)

var detectCycle = function(head) {
    if(head===null||head.next===null) return null;
    let fast=head,slow=head;
    do{
       if(fast.next===null||fast.next.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;
};

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章