2.8 Loop Detection

Given a circular linked list, implement an algorithm that returns the node at the beginning of the loop.
DEFINITION
Circular linked list: A (corrupt) linked list in which a node's next pointer points to an earlier node, so as to make a loop in the linked list.
EXAMPLE
Input: A -> B -> C -> D -> E -> C [the same C as earlier]
Output: C

Hint
  • There are really two parts to this problem. First, detect if the linked list has a loop. Second, figure out where the loop starts.
  • To identify if there's a cycle, try the "runner" approach described on page 93. Have one pointer move faster than the other.
  • You can use two pointers, one moving twice as fast as the other. If there is a cycle, the two pointers will collide. They will land at the same location at the same time. Where do they land? Why there?
  • If you haven't identified the pattern of where the two pointers start, try this: Use the linked list 1->2->3->4->5->6->7->8->9->?, where the? links to another node. Try making the? the first node (that is, the 9 points to the 1 such that the entire linked list is a loop). Then make the ? the node 2. Then the node 3. Then the node 4. What is the pattern? Can you explain why this happens?
Solution

本題需要我們判斷鏈表內(nèi)是否有環(huán)缩宜,并且返回環(huán)的起始結(jié)點。依然是用雙指針的方法圆兵,慢指針每次移動1步超凳,快指針每次移動2步,若有環(huán),則兩個指針必然會中途相遇。然后將其中一個指針從頭開始遍歷,這時兩個指針同步移動1虫蝶,當它們再次相遇時便是環(huán)的起始結(jié)點。

public ListNode detectCycle(ListNode head) {
    ListNode slow = head, fast = head;
    while (fast != null && fast.next != null) {
        slow = slow.next;
        fast = fast.next.next;
        if (slow == fast) break;
    }
    if (fast == null || fast.next == null) return null;
    slow = head;
    while (slow != fast) {
        slow = slow.next;
        fast = fast.next;
    }
    return slow;
}
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖楞遏,帶你破解...
    沈念sama閱讀 217,509評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件网缝,死亡現(xiàn)場離奇詭異扼仲,居然都是意外死亡,警方通過查閱死者的電腦和手機夜涕,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評論 3 394
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事癞季》夏溃” “怎么了?”我有些...
    開封第一講書人閱讀 163,875評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長褒翰。 經(jīng)常有香客問我抡医,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,441評論 1 293
  • 正文 為了忘掉前任安疗,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘铁追。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 67,488評論 6 392
  • 文/花漫 我一把揭開白布高每。 她就那樣靜靜地躺著浴井,像睡著了一般。 火紅的嫁衣襯著肌膚如雪不脯。 梳的紋絲不亂的頭發(fā)上亿昏,一...
    開封第一講書人閱讀 51,365評論 1 302
  • 那天峦剔,我揣著相機與錄音,去河邊找鬼角钩。 笑死吝沫,一個胖子當著我的面吹牛呻澜,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播野舶,決...
    沈念sama閱讀 40,190評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼易迹,長吁一口氣:“原來是場噩夢啊……” “哼宰衙!你這毒婦竟也來了平道?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,062評論 0 276
  • 序言:老撾萬榮一對情侶失蹤供炼,失蹤者是張志新(化名)和其女友劉穎一屋,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體袋哼,經(jīng)...
    沈念sama閱讀 45,500評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡冀墨,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,706評論 3 335
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了涛贯。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片诽嘉。...
    茶點故事閱讀 39,834評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖弟翘,靈堂內(nèi)的尸體忽然破棺而出虫腋,到底是詐尸還是另有隱情,我是刑警寧澤稀余,帶...
    沈念sama閱讀 35,559評論 5 345
  • 正文 年R本政府宣布悦冀,位于F島的核電站,受9級特大地震影響睛琳,放射性物質(zhì)發(fā)生泄漏盒蟆。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,167評論 3 328
  • 文/蒙蒙 一师骗、第九天 我趴在偏房一處隱蔽的房頂上張望历等。 院中可真熱鬧,春花似錦辟癌、人聲如沸寒屯。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,779評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽浩螺。三九已至,卻和暖如春仍侥,著一層夾襖步出監(jiān)牢的瞬間要出,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,912評論 1 269
  • 我被黑心中介騙來泰國打工农渊, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留患蹂,地道東北人或颊。 一個月前我還...
    沈念sama閱讀 47,958評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像传于,于是被迫代替她去往敵國和親囱挑。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,779評論 2 354

推薦閱讀更多精彩內(nèi)容