{"id":318603,"date":"2021-02-05T05:46:09","date_gmt":"2021-02-04T21:46:09","guid":{"rendered":"http:\/\/4563.org\/?p=318603"},"modified":"2021-02-05T05:46:09","modified_gmt":"2021-02-04T21:46:09","slug":"%e5%be%ae%e8%bd%af%e9%9d%a2%e8%af%95%e9%a2%98%ef%bc%9a%e5%b8%a6%e7%8e%af%e9%93%be%e8%a1%a8-ii","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=318603","title":{"rendered":"\u5fae\u8f6f\u9762\u8bd5\u9898\uff1a\u5e26\u73af\u94fe\u8868 II"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u5fae\u8f6f\u9762\u8bd5\u9898\uff1a\u5e26\u73af\u94fe\u8868 II               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zzzrf <\/span>  <span><i><\/i> 8<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<h3>\u63cf\u8ff0<\/h3>\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u94fe\u8868\uff0c\u5982\u679c\u94fe\u8868\u4e2d\u5b58\u5728\u73af\uff0c\u5219\u8fd4\u56de\u5230\u94fe\u8868\u4e2d\u73af\u7684\u8d77\u59cb\u8282\u70b9\uff0c\u5982\u679c\u6ca1\u6709\u73af\uff0c\u8fd4\u56de null \u3002<\/p>\n<p>\u5728\u7ebf\u8bc4\u6d4b\u5730\u5740<\/p>\n<h3>\u6837\u4f8b 1<\/h3>\n<pre><code>\u8f93\u5165\uff1anull,no cycle \u8f93\u51fa\uff1ano cycle \u89e3\u91ca\uff1a \u94fe\u8868\u4e3a\u7a7a\uff0c\u6240\u4ee5\u6ca1\u6709\u73af\u5b58\u5728\u3002 <\/code><\/pre>\n<h3>\u6837\u4f8b 2<\/h3>\n<pre><code>\u8f93\u5165\uff1a-21-&gt;10-&gt;4-&gt;5\uff0ctail connects to node index 1 \u8f93\u51fa\uff1a10 \u89e3\u91ca\uff1a \u6700\u540e\u4e00\u4e2a\u8282\u70b9 5 \u6307\u5411\u4e0b\u6807\u4e3a 1 \u7684\u8282\u70b9\uff0c\u4e5f\u5c31\u662f 10\uff0c\u6240\u4ee5\u73af\u7684\u5165\u53e3\u4e3a 10 \u3002 <\/code><\/pre>\n<h3>\u7b97\u6cd5\u601d\u8def\uff1a<\/h3>\n<p>Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up: Can you solve it without using extra space?<\/p>\n<h3>\u8003\u70b9\uff1a<\/h3>\n<ul>\n<li>\u53cc\u6307\u9488\u94fe\u8868\u5224\u73af<\/li>\n<\/ul>\n<h3>\u9898\u89e3\uff1a<\/h3>\n<ul>\n<li>\u4f7f\u7528\u53cc\u6307\u9488\u5224\u65ad\u94fe\u8868\u4e2d\u662f\u5426\u6709\u73af\uff0c\u6162\u6307\u9488\u6bcf\u6b21\u8d70\u4e00\u6b65\uff0c\u5feb\u6307\u9488\u6bcf\u6b21\u8d70\u4e24\u6b65\uff0c\u82e5\u94fe\u8868\u4e2d\u6709\u73af\uff0c\u5219\u4e24\u6307\u9488\u5fc5\u5b9a\u76f8\u9047\u3002<\/li>\n<li>\u5047\u8bbe\u73af\u7684\u957f\u5ea6\u4e3a l\uff0c\u73af\u4e0a\u5165\u53e3\u8ddd\u79bb\u94fe\u8868\u5934\u8ddd\u79bb\u4e3a a\uff0c\u4e24\u6307\u9488\u7b2c\u4e00\u6b21\u76f8\u9047\u5904\u8ddd\u79bb\u73af\u5165\u53e3\u4e3a b\uff0c\u5219\u53e6\u4e00\u6bb5\u73af\u7684\u957f\u5ea6\u4e3a c=l-b\uff0c\u7531\u4e8e\u5feb\u6307\u9488\u8d70\u8fc7\u7684\u8ddd\u79bb\u662f\u6162\u6307\u9488\u7684\u4e24\u500d\uff0c\u5219\u6709 a+l+b=2*(a+b),\u53c8\u6709 l=b+c\uff0c\u53ef\u5f97 a=c\uff0c\u6545\u5f53\u5224\u65ad\u6709\u73af\u65f6(slow==fast)\u65f6\uff0c\u79fb\u52a8\u5934\u6307\u9488\uff0c\u540c\u65f6\u79fb\u52a8\u6162\u6307\u9488\uff0c\u4e24\u6307\u9488\u76f8\u9047\u5904\u5373\u4e3a\u73af\u7684\u5165\u53e3\u3002<\/li>\n<\/ul>\n<pre><code>\/**  * Definition for ListNode.  * public class ListNode {  *     int val;  *     ListNode next;  *     ListNode(int val) {  *         this.val = val;  *         this.next = null;  *     }  * }  *\/  public class Solution {     public ListNode detectCycle(ListNode head) {         if (head == null || head.next==null) {             return null;         }          ListNode fast, slow;         fast = head.next;                        \/\/\u5feb\u6307\u9488         slow = head;                                \/\/\u6162\u6307\u9488         while (fast != slow) {                \/\/\u5feb\u6162\u6307\u9488\u76f8\u9047             if(fast==null || fast.next==null)                 return null;             fast = fast.next.next;             slow = slow.next;         }                   while (head != slow.next) {  \/\/\u540c\u65f6\u79fb\u52a8 head \u548c\u6162\u6307\u9488             head = head.next;             slow = slow.next;         }         return head;                                \/\/\u4e24\u6307\u9488\u76f8\u9047\u5904\u5373\u4e3a\u73af\u7684\u5165\u53e3     } } <\/code><\/pre>\n<p>\u66f4\u591a\u9898\u89e3\u53c2\u8003<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>0<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u5fae\u8f6f\u9762\u8bd5\u9898\uff1a\u5e26\u73af\u94fe\u8868 II \u8cc7\u6df1\u5927&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/318603"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=318603"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/318603\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=318603"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=318603"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=318603"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}