{"id":2487,"date":"2022-03-25T10:54:13","date_gmt":"2022-03-25T02:54:13","guid":{"rendered":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=2487"},"modified":"2022-03-25T10:54:13","modified_gmt":"2022-03-25T02:54:13","slug":"%e7%ae%97%e6%b3%95%e5%85%a5%e9%97%a8%e7%ac%ac%e4%ba%94%e5%a4%a9","status":"publish","type":"post","link":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=2487","title":{"rendered":"\u7b97\u6cd5\u5165\u95e8\u7b2c\u4e94\u5929"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode-cn.com\/study-plan\/algorithms\/?progress=7ob9vsg\">\u300c\u7b97\u6cd5\u300d &#8211; \u5b66\u4e60\u8ba1\u5212 &#8211; \u529b\u6263\uff08LeetCode\uff09\u5168\u7403\u6781\u5ba2\u631a\u7231\u7684\u6280\u672f\u6210\u957f\u5e73\u53f0 (leetcode-cn.com)<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/leetcode-cn.com\/problems\/middle-of-the-linked-list\/\">876. \u94fe\u8868\u7684\u4e2d\u95f4\u7ed3\u70b9<\/a><\/p>\n\n\n\n<p>\u7ecf\u5178\u9898\u578b\uff0c\u53cc\u6307\u9488\uff0c\u5feb\u6162\u6307\u9488<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>\/**<br><\/em><em> 876. \u94fe\u8868\u7684\u4e2d\u95f4\u7ed3\u70b9<br><\/em><em> \u7ed9\u5b9a\u4e00\u4e2a\u5934\u7ed3\u70b9\u4e3a head \u7684\u975e\u7a7a\u5355\u94fe\u8868\uff0c\u8fd4\u56de\u94fe\u8868\u7684\u4e2d\u95f4\u7ed3\u70b9\u3002<br><\/em><em><br><\/em><em> \u5982\u679c\u6709\u4e24\u4e2a\u4e2d\u95f4\u7ed3\u70b9\uff0c\u5219\u8fd4\u56de\u7b2c\u4e8c\u4e2a\u4e2d\u95f4\u7ed3\u70b9\u3002<br><\/em><em><br><\/em><em><br><\/em><em><br><\/em><em> \u793a\u4f8b 1\uff1a<br><\/em><em><br><\/em><em> \u8f93\u5165\uff1a[1,2,3,4,5]<br><\/em><em> \u8f93\u51fa\uff1a\u6b64\u5217\u8868\u4e2d\u7684\u7ed3\u70b9 3 (\u5e8f\u5217\u5316\u5f62\u5f0f\uff1a[3,4,5])<br><\/em><em> \u8fd4\u56de\u7684\u7ed3\u70b9\u503c\u4e3a 3 \u3002 (\u6d4b\u8bc4\u7cfb\u7edf\u5bf9\u8be5\u7ed3\u70b9\u5e8f\u5217\u5316\u8868\u8ff0\u662f [3,4,5])\u3002<br><\/em><em> \u6ce8\u610f\uff0c\u6211\u4eec\u8fd4\u56de\u4e86\u4e00\u4e2a ListNode \u7c7b\u578b\u7684\u5bf9\u8c61 ans\uff0c\u8fd9\u6837\uff1a<br><\/em><em> ans.val = 3, ans.next.val = 4, ans.next.next.val = 5, \u4ee5\u53ca ans.next.next.next = NULL.<br><\/em><em> \u793a\u4f8b 2\uff1a<br><\/em><em><br><\/em><em> \u8f93\u5165\uff1a[1,2,3,4,5,6]<br><\/em><em> \u8f93\u51fa\uff1a\u6b64\u5217\u8868\u4e2d\u7684\u7ed3\u70b9 4 (\u5e8f\u5217\u5316\u5f62\u5f0f\uff1a[4,5,6])<br><\/em><em> \u7531\u4e8e\u8be5\u5217\u8868\u6709\u4e24\u4e2a\u4e2d\u95f4\u7ed3\u70b9\uff0c\u503c\u5206\u522b\u4e3a 3 \u548c 4\uff0c\u6211\u4eec\u8fd4\u56de\u7b2c\u4e8c\u4e2a\u7ed3\u70b9\u3002<br><\/em><em><br><\/em><em><br><\/em><em> \u63d0\u793a\uff1a<br><\/em><em><br><\/em><em> \u7ed9\u5b9a\u94fe\u8868\u7684\u7ed3\u70b9\u6570\u4ecb\u4e8e 1 \u548c 100 \u4e4b\u95f4\u3002<br><\/em><em><br><\/em><em> * <\/em><strong><em>@author <\/em><\/strong><em>zeroheart<br><\/em><em> *<br><\/em><em> *\/<br><\/em>public class Question876 {<br>    public ListNode middleNode(ListNode head) {<br>        \/\/ \u51fa\u73b0\u4e86\uff0c\u5feb\u6162\u6307\u9488\u7684\u4f8b\u5b50\uff0c\u4e24\u4e2a\u6307\u9488\uff0c\u4e00\u4e2a\u6b65\u8fdb1\uff0c\u4e00\u4e2a\u6b65\u8fdb2\uff0c\u6700\u7ec82\u5230\u8fbe\u672b\u5c3e\u7684\u65f6\u5019\uff0c1\u5c31\u5728\u4e2d\u95f4<br>        ListNode slow = head, fast = head;<br>        while (slow!=null &amp;&amp; fast!=null &amp;&amp; fast.next!=null){<br>            slow = slow.next;<br>            fast = fast.next.next;<br>        }<br>        return slow;<br>    }<br>}<\/pre>\n\n\n\n<p><a href=\"https:\/\/leetcode-cn.com\/problems\/remove-nth-node-from-end-of-list\/\">19. \u5220\u9664\u94fe\u8868\u7684\u5012\u6570\u7b2c N \u4e2a\u7ed3\u70b9<\/a><\/p>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">\u8fd9\u9898\u4e5f\u662f\u53cc\u6307\u9488\uff0c\u5feb\u6307\u9488\u8d70n\u6b65\u4e4b\u540e\uff0c\u5728\u4e00\u6b65\u4e00\u6b65\u8d70\uff0c\u6709\u4e2a\u53d8\u5316\uff0c\u6ce8\u610f\u5bf9next\u7684\u5904\u7406\u7684\u4e34\u754c\u70b9<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>\/**<br><\/em><em> 19. \u5220\u9664\u94fe\u8868\u7684\u5012\u6570\u7b2c N \u4e2a\u7ed3\u70b9<br><\/em><em> \u7ed9\u4f60\u4e00\u4e2a\u94fe\u8868\uff0c\u5220\u9664\u94fe\u8868\u7684\u5012\u6570\u7b2c n \u4e2a\u7ed3\u70b9\uff0c\u5e76\u4e14\u8fd4\u56de\u94fe\u8868\u7684\u5934\u7ed3\u70b9\u3002<br><\/em><em><br><\/em><em> \u793a\u4f8b 1\uff1a<br><\/em><em><br><\/em><em> \u8f93\u5165\uff1ahead = [1,2,3,4,5], n = 2<br><\/em><em> \u8f93\u51fa\uff1a[1,2,3,5]<br><\/em><em> \u793a\u4f8b 2\uff1a<br><\/em><em><br><\/em><em> \u8f93\u5165\uff1ahead = [1], n = 1<br><\/em><em> \u8f93\u51fa\uff1a[]<br><\/em><em> \u793a\u4f8b 3\uff1a<br><\/em><em><br><\/em><em> \u8f93\u5165\uff1ahead = [1,2], n = 1<br><\/em><em> \u8f93\u51fa\uff1a[1]<br><\/em><em><br><\/em><em><br><\/em><em> \u63d0\u793a\uff1a<br><\/em><em><br><\/em><em> \u94fe\u8868\u4e2d\u7ed3\u70b9\u7684\u6570\u76ee\u4e3a sz<br><\/em><em> 1 &lt;= sz &lt;= 30<br><\/em><em> 0 &lt;= Node.val &lt;= 100<br><\/em><em> 1 &lt;= n &lt;= sz<br><\/em><em><br><\/em><em> * <\/em><strong><em>@author <\/em><\/strong><em>zeroheart<br><\/em><em> *<br><\/em><em> *\/<br><\/em>public class Question19 {<br>    public ListNode removeNthFromEnd(ListNode head, int n) {<br>        \/\/ \u53cc\u6307\u9488\uff0c\u5feb\u7684\u8d70n\u6b65\uff0c\u7136\u540e\u4e00\u6b21\u4e00\u6b65\uff0c\u6162\u7684\u4e00\u6b65\u6b65\u6765\uff0c\u5feb\u7684\u8d70\u5b8c\u4e86\uff0c\u6162\u7684\u5c31\u5230\u4e86\u5012\u6570\u7b2cn\u7684\u4f4d\u7f6e<br>        ListNode slow = head, fast = head;<br>        for(int i = 0; i&lt;n; i++){<br>            if(fast!=null) {<br>                fast = fast.next;<br>            }<br>        }<br>        if(fast==null) {<br>            return head.next;<br>        }<br><br>        while (fast.next!=null){<br>            fast = fast.next;<br>            slow = slow.next;<br>        }<br><br>        \/\/\u53ea\u62ff\u6389\u4e00\u4e2a\u70b9<br>        slow.next = slow.next.next;<br><br>        return head;<br>    }<br>}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u300c\u7b97\u6cd5\u300d &#8211; \u5b66\u4e60\u8ba1\u5212 &#8211; \u529b\u6263\uff08LeetCode\uff09\u5168\u7403\u6781\u5ba2\u631a\u7231\u7684\u6280\u672f\u6210\u957f\u5e73\u53f0 (le [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[155],"tags":[157,164],"_links":{"self":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/2487"}],"collection":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2487"}],"version-history":[{"count":9,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/2487\/revisions"}],"predecessor-version":[{"id":2496,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/2487\/revisions\/2496"}],"wp:attachment":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2487"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2487"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2487"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}