{"id":1932,"date":"2016-04-01T13:54:24","date_gmt":"2016-04-01T05:54:24","guid":{"rendered":"http:\/\/boweihe.me\/?p=1932"},"modified":"2016-04-01T13:54:24","modified_gmt":"2016-04-01T05:54:24","slug":"leetcode-142-linked-list-cycle-ii","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=1932","title":{"rendered":"LeetCode 142. Linked List Cycle II"},"content":{"rendered":"<p>Given a linked list, return the node where the cycle begins. If there is no cycle, return <code>null<\/code>.<br \/>\n<b>Note:<\/b> Do not modify the linked list.<br \/>\n<b>Follow up<\/b>:<br \/>\nCan you solve it without using extra space?<\/p>\n<h3>\u601d\u8def<\/h3>\n<p><em>\u5176\u5b9e\u8fd8\u6709\u66f4\u7b80\u5355\u7684\u65b9\u6cd5\uff0c\u5728\u300aCracking the Coding Interview\u300b\u4e0a\u6709\u63d0\u5230\u3002\u901a\u8fc7\u6570\u5b66\u8ba1\u7b97\u53ef\u4ee5\u63a8\u5bfc\u51fa\u6765\uff0c\u7528\u5feb\u884c\u6307\u9488\u7684\u65b9\u6cd5\u5224\u65ad\u51fa\u73af\u540e\uff0c\u53ef\u4ee5\u4e0d\u7528\u8ba1\u7b97\u73af\u5927\u5c0f\uff0c\u628a\u5feb\u6307\u9488\u53d8\u6162\u7136\u540e\u653e\u5230\u94fe\u5934\u4e0a\uff0c\u4e24\u4e2a\u6307\u9488\u7ee7\u7eed\u8d70\u5230\u76f8\u78b0\u5c31\u5269\u5165\u53e3\u4e86\u3002<\/em><br \/>\n\u5206\u4e3a\u4e09\u6b65\uff1a\u5224\u65ad\u73af\uff0c\u8ba1\u7b97\u73af\u7684\u5927\u5c0f\uff0c\u627e\u5230\u5165\u53e3<br \/>\n\u5224\u65ad\u73af\u5c31\u662f\u8001\u4e00\u5957\uff0c\u75282\u500d\u901f\u6307\u9488\u5c31\u884c\uff0c\u8fd9\u4e2a\u4e4b\u524d\u7684\u9898\u76ee\u91cc\u5934\u6709\uff1b<br \/>\n\u8ba1\u7b97\u73af\u7684\u5927\u5c0fk\uff0c\u73af\u7684\u5927\u5c0f\u4e48\uff0c\u521a\u624d\u76f8\u78b0\u7684\u5730\u65b9\u627e\u4e2a\u6307\u9488\u7ed5\u5708\u5708\u8d70\u3002\u8bb0\u4e2a\u6570\uff0c\u518d\u78b0\u5230\u76f8\u9047\u5730\u70b9\u5c31\u662f\u73af\u5927\u5c0f\u4e86\uff1b<br \/>\n\u627e\u5230\u5165\u53e3\u540e\uff0c\u6211\u4eec\u6709\u4e24\u4e2a\u666e\u901a\u901f\u5ea6\u7684\u6307\u9488pA,pB\uff0c\u8ba9\u5176\u4e2d\u4e00\u4e2apA\u5148\u5f80\u524d\u8d70k\u6b65\uff0c\u7136\u540e\u4e24\u4e2a\u4e00\u8d77\u5f00\u59cb\u632a\u52a8\uff0c\u53ef\u4ee5\u77e5\u9053\uff0c\u5982\u679cpA\u4e0epB\u53c8\u76f8\u9047\u4e86\uff0c\u90a3\u76f8\u9047\u70b9\u5c31\u662f\u73af\u7684\u5165\u53e3\u4e86\u3002\u6b64\u65f6pA\u6bd4pB\u521a\u597d\u591a\u8d70\u4e86\u4e00\u6574\u5708\u3002<\/p>\n<h3>\u4ee3\u7801<\/h3>\n<pre class=\"lang:c++ decode:true \">\/**\n * Definition for singly-linked list.\n * struct ListNode {\n *     int val;\n *     ListNode *next;\n *     ListNode(int x) : val(x), next(NULL) {}\n * };\n *\/\nclass Solution {\npublic:\n    ListNode* meetingNode(ListNode* head){\n        \/\/Find the meeting node\n        if(head == NULL)\n            return NULL;\n        ListNode* slow = head-&gt;next;\n        if(slow == NULL)\n            return NULL;\n        ListNode* fast = slow-&gt;next;\n        while(fast != NULL &amp;&amp; slow != NULL){\n            if (slow == fast)\n                return slow;\n            slow = slow-&gt;next;\n            fast = fast-&gt;next;\n            if(fast != NULL)\n                fast = fast-&gt;next;\n        }\n        return NULL;\n    }\n    ListNode *detectCycle(ListNode *head) {\n        ListNode* meet = meetingNode(head);\n        if(meet == NULL)\n            return NULL;\n        \/\/Get loop size\n        ListNode* temp = meet-&gt;next;\n        int count = 1;\n        while(temp != meet){\n            count++;\n            temp = temp-&gt;next;\n        }\n        \/\/Pick one pointer at start, move `count` steps forward\n        temp = head;\n        meet = head;\n        while(count &gt; 0){\n            count--;\n            meet = meet-&gt;next;\n        }\n        \/\/Find the cycle start\n        while(temp != meet){\n            temp = temp-&gt;next;\n            meet = meet-&gt;next;\n        }\n        return meet;\n    }\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note: Do not modify the linked list. Follow up: Can you solve it without using extra space? \u601d\u8def \u5176\u5b9e\u8fd8\u6709\u66f4\u7b80\u5355\u7684\u65b9\u6cd5\uff0c\u5728\u300aCracking the Coding Interview\u300b\u4e0a\u6709\u63d0\u5230\u3002\u901a\u8fc7\u6570\u5b66\u8ba1\u7b97\u53ef\u4ee5\u63a8\u5bfc\u51fa\u6765\uff0c\u7528\u5feb\u884c\u6307\u9488\u7684\u65b9\u6cd5\u5224\u65ad\u51fa\u73af\u540e\uff0c\u53ef\u4ee5\u4e0d\u7528\u8ba1\u7b97\u73af\u5927\u5c0f\uff0c\u628a\u5feb\u6307\u9488\u53d8\u6162\u7136\u540e\u653e\u5230\u94fe\u5934\u4e0a\uff0c\u4e24\u4e2a\u6307\u9488\u7ee7\u7eed\u8d70\u5230\u76f8\u78b0\u5c31\u5269\u5165\u53e3\u4e86\u3002 \u5206\u4e3a\u4e09\u6b65\uff1a\u5224\u65ad\u73af\uff0c\u8ba1\u7b97\u73af\u7684\u5927\u5c0f\uff0c\u627e\u5230\u5165\u53e3 \u5224\u65ad\u73af\u5c31\u662f\u8001\u4e00\u5957\uff0c\u75282\u500d\u901f\u6307\u9488\u5c31\u884c\uff0c\u8fd9\u4e2a\u4e4b\u524d\u7684\u9898\u76ee\u91cc\u5934\u6709\uff1b \u8ba1\u7b97\u73af\u7684\u5927\u5c0fk\uff0c\u73af\u7684\u5927\u5c0f\u4e48\uff0c\u521a\u624d\u76f8\u78b0\u7684\u5730\u65b9\u627e\u4e2a\u6307\u9488\u7ed5\u5708\u5708\u8d70\u3002\u8bb0\u4e2a\u6570\uff0c\u518d\u78b0\u5230\u76f8\u9047\u5730\u70b9\u5c31\u662f\u73af\u5927\u5c0f\u4e86\uff1b \u627e\u5230\u5165\u53e3\u540e\uff0c\u6211\u4eec\u6709\u4e24\u4e2a\u666e\u901a\u901f\u5ea6\u7684\u6307\u9488pA,pB\uff0c\u8ba9\u5176\u4e2d\u4e00\u4e2apA\u5148\u5f80\u524d\u8d70k\u6b65\uff0c\u7136\u540e\u4e24\u4e2a\u4e00\u8d77\u5f00\u59cb\u632a\u52a8\uff0c\u53ef\u4ee5\u77e5\u9053\uff0c\u5982\u679cpA\u4e0epB\u53c8\u76f8\u9047\u4e86\uff0c\u90a3\u76f8\u9047\u70b9\u5c31\u662f\u73af\u7684\u5165\u53e3\u4e86\u3002\u6b64\u65f6pA\u6bd4pB\u521a\u597d\u591a\u8d70\u4e86\u4e00\u6574\u5708\u3002 \u4ee3\u7801 \/** * Definition for singly-linked list. * struct ListNode { [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[66],"class_list":["post-1932","post","type-post","status-publish","format-standard","hentry","category-study","tag-leetcode-oj"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/1932","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1932"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/1932\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1932"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1932"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1932"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}