{"id":495,"date":"2020-09-26T10:48:30","date_gmt":"2020-09-26T02:48:30","guid":{"rendered":"http:\/\/39.96.58.60\/?p=495"},"modified":"2022-10-18T16:39:03","modified_gmt":"2022-10-18T08:39:03","slug":"leetcode-142-%e7%8e%af%e5%bd%a2%e9%93%be%e8%a1%a8-ii","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=495","title":{"rendered":"Leetcode 142. \u73af\u5f62\u94fe\u8868 II"},"content":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u94fe\u8868\uff0c\u8fd4\u56de\u94fe\u8868\u5f00\u59cb\u5165\u73af\u7684\u7b2c\u4e00\u4e2a\u8282\u70b9\u3002&nbsp;\u5982\u679c\u94fe\u8868\u65e0\u73af\uff0c\u5219\u8fd4\u56de&nbsp;<code>null<\/code>\u3002<\/p>\n<p>\u4e3a\u4e86\u8868\u793a\u7ed9\u5b9a\u94fe\u8868\u4e2d\u7684\u73af\uff0c\u6211\u4eec\u4f7f\u7528\u6574\u6570 <code>pos<\/code> \u6765\u8868\u793a\u94fe\u8868\u5c3e\u8fde\u63a5\u5230\u94fe\u8868\u4e2d\u7684\u4f4d\u7f6e\uff08\u7d22\u5f15\u4ece 0 \u5f00\u59cb\uff09\u3002 \u5982\u679c <code>pos<\/code> \u662f <code>-1<\/code>\uff0c\u5219\u5728\u8be5\u94fe\u8868\u4e2d\u6ca1\u6709\u73af\u3002<\/p>\n<p><strong>\u8bf4\u660e\uff1a<\/strong>\u4e0d\u5141\u8bb8\u4fee\u6539\u7ed9\u5b9a\u7684\u94fe\u8868\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>head = [3,2,0,-4], pos = 1\n\n<strong>\u8f93\u51fa\uff1a<\/strong>tail connects to node index 1\n\n<strong>\u89e3\u91ca\uff1a<\/strong>\u94fe\u8868\u4e2d\u6709\u4e00\u4e2a\u73af\uff0c\u5176\u5c3e\u90e8\u8fde\u63a5\u5230\u7b2c\u4e8c\u4e2a\u8282\u70b9\u3002\n\n<\/pre>\n<p><img alt=\"\" src=\"https:\/\/assets.leetcode-cn.com\/aliyun-lc-upload\/uploads\/2018\/12\/07\/circularlinkedlist.png\" style=\"height: 97px; width: 300px;\"><\/p>\n<p><strong>\u793a\u4f8b&nbsp;2\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>head = [1,2], pos = 0\n\n<strong>\u8f93\u51fa\uff1a<\/strong>tail connects to node index 0\n\n<strong>\u89e3\u91ca\uff1a<\/strong>\u94fe\u8868\u4e2d\u6709\u4e00\u4e2a\u73af\uff0c\u5176\u5c3e\u90e8\u8fde\u63a5\u5230\u7b2c\u4e00\u4e2a\u8282\u70b9\u3002\n\n<\/pre>\n<p><img alt=\"\" src=\"https:\/\/assets.leetcode-cn.com\/aliyun-lc-upload\/uploads\/2018\/12\/07\/circularlinkedlist_test2.png\" style=\"height: 74px; width: 141px;\"><\/p>\n<p><strong>\u793a\u4f8b 3\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>head = [1], pos = -1\n\n<strong>\u8f93\u51fa\uff1a<\/strong>no cycle\n\n<strong>\u89e3\u91ca\uff1a<\/strong>\u94fe\u8868\u4e2d\u6ca1\u6709\u73af\u3002\n\n<\/pre>\n<p><img alt=\"\" src=\"https:\/\/assets.leetcode-cn.com\/aliyun-lc-upload\/uploads\/2018\/12\/07\/circularlinkedlist_test3.png\" style=\"height: 45px; width: 45px;\"><\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u8fdb\u9636\uff1a<\/strong><\/p>\n<p>\u4f60\u662f\u5426\u53ef\u4ee5\u4e0d\u7528\u989d\u5916\u7a7a\u95f4\u89e3\u51b3\u6b64\u9898\uff1f<\/p>\n<p>**\u96be\u5ea6**: Medium<\/p>\n<p>**\u6807\u7b7e**: \u94fe\u8868\u3001 \u53cc\u6307\u9488\u3001<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-python\" data-lang=\"Python\"><code>\n# -*- coding: utf-8 -*-\n# @Author  : LG\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a52 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8698.32% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a16.3 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8674.63% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u5feb\u6162\u6307\u9488\n\n    |&lt;----    l1   ----&gt;|&lt; --------\n    1   2   3   4   5   6   7   8   l2\n                        15      9   \u2193\n                        14      10  __\n                        13  12  11\n\n    \u4ece\u5934\u90e8\u5230\u5165\u73af \u8ddd\u79bb\u4e3a l1\uff0c \u5165\u73af\u5230\u5feb\u6162\u6307\u9488\u76f8\u9047\u70b9\u4e3al2\uff0c \u73af\u957f c\n\n    \u5219\uff0c \u6162\u6307\u9488\u8d70\u8fc7\u7684\u8ddd\u79bb\u4e3a s = 1 + l1 + l2\n        \u5feb\u6307\u9488\u8d70\u8fc7\u7684\u8ddd\u79bb\u4e3a f = l1 + l2 + nc  \u7ed5\u4e86\u73af\u5185\u8d70\u4e86n\u5708\n\n    2s = f\n    \u5219\uff0c  2 + 2l1 + 2l2 = l1 + l2 + nc\n\n    \u6240\u6c42\u5165\u73af\u5904 l1 = nc - l2 - 2\n    nc - l2 \u53ef\u7531 \u6162\u6307\u9488\u4ece\u76f8\u9047\u5904 \u5f00\u59cb\u7ed5\u73af\u8d70\u5230\u5165\u73af\u5904\u5f97\u6765\u3002\n    \u5373 \u5728\u76f8\u9047\u65f6\uff0c\u4ece\u8d77\u70b9\u524d\u4e24\u4e2a\u8ddd\u79bb\u5f00\u59cb\uff0c\u521d\u59cb\u5316\u4e00\u4e2a\u6307\u9488\uff0c \u540c\u65f6\u6162\u6307\u9488\u4ece\u76f8\u9047\u5904\u7ee7\u7eed\u8d70\u3002 \u65b0\u6307\u9488\u4e0e\u6162\u6307\u9488\u4f1a\u5728\u5165\u73af\u5904\u76f8\u9047\n\"\"\"\nclass Solution:\n    def detectCycle(self, head: ListNode) -&gt; ListNode:\n        s, f = ListNode(0, next= head), head    # \u5feb\u6162\u6307\u9488\n\n        while s != f:   # \u76f8\u9047\u5219\u6709\u73af\uff0c\u4e14\u5f53\u524d\u4f4d\u7f6e\u4e3a\u76f8\u9047\u5904\n            if f and f.next:\n                s = s.next\n                f = f.next.next\n            else:   # \u65e0\u73af\n                return\n        # \u6709\u73af\u65f6\uff0c\u521d\u59cb\u5316\u4e00\u4e2a\u65b0\u6307\u9488\uff0c\u5728\u5f00\u59cb\u524d\u4e24\u4e2a\u8ddd\u79bb\u5904\n        new = ListNode(0, next=ListNode(0, next = head))    # \u65b0\u6307\u9488\n\n        while new != s: # \u65b0\u6307\u9488\u4e0e\u6162\u6307\u9488\u540c\u65f6\u8d70\uff0c\u76f8\u9047\u5904\u4fbf\u662f\u5165\u73af\u70b9\n            new = new.next\n            s = s.next\n\n        return s<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u94fe\u8868\uff0c\u8fd4\u56de\u94fe\u8868\u5f00\u59cb\u5165\u73af\u7684\u7b2c\u4e00\u4e2a\u8282\u70b9\u3002&nbsp;\u5982\u679c\u94fe\u8868\u65e0\u73af\uff0c\u5219\u8fd4\u56de&nbsp;null\u3002 \u4e3a\u4e86\u8868\u793a\u7ed9&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":[],"categories":[11,1],"tags":[],"_links":{"self":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/495"}],"collection":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=495"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/495\/revisions"}],"predecessor-version":[{"id":496,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/495\/revisions\/496"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=495"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=495"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=495"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}