{"id":635,"date":"2020-09-26T11:05:47","date_gmt":"2020-09-26T03:05:47","guid":{"rendered":"http:\/\/39.96.58.60\/?p=635"},"modified":"2022-10-18T16:38:35","modified_gmt":"2022-10-18T08:38:35","slug":"leetcode-19-%e5%88%a0%e9%99%a4%e9%93%be%e8%a1%a8%e7%9a%84%e5%80%92%e6%95%b0%e7%ac%acn%e4%b8%aa%e8%8a%82%e7%82%b9","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=635","title":{"rendered":"Leetcode 19. \u5220\u9664\u94fe\u8868\u7684\u5012\u6570\u7b2cN\u4e2a\u8282\u70b9"},"content":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u94fe\u8868\uff0c\u5220\u9664\u94fe\u8868\u7684\u5012\u6570\u7b2c&nbsp;<em>n&nbsp;<\/em>\u4e2a\u8282\u70b9\uff0c\u5e76\u4e14\u8fd4\u56de\u94fe\u8868\u7684\u5934\u7ed3\u70b9\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>\u7ed9\u5b9a\u4e00\u4e2a\u94fe\u8868: <strong>1-&gt;2-&gt;3-&gt;4-&gt;5<\/strong>, \u548c <strong><em>n<\/em> = 2<\/strong>.\n\n\n\n\u5f53\u5220\u9664\u4e86\u5012\u6570\u7b2c\u4e8c\u4e2a\u8282\u70b9\u540e\uff0c\u94fe\u8868\u53d8\u4e3a <strong>1-&gt;2-&gt;3-&gt;5<\/strong>.\n\n<\/pre>\n<p><strong>\u8bf4\u660e\uff1a<\/strong><\/p>\n<p>\u7ed9\u5b9a\u7684 <em>n<\/em>&nbsp;\u4fdd\u8bc1\u662f\u6709\u6548\u7684\u3002<\/p>\n<p><strong>\u8fdb\u9636\uff1a<\/strong><\/p>\n<p>\u4f60\u80fd\u5c1d\u8bd5\u4f7f\u7528\u4e00\u8d9f\u626b\u63cf\u5b9e\u73b0\u5417\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# Definition for singly-linked list.\n# class ListNode:\n#     def __init__(self, x):\n#         self.val = x\n#         self.next = None\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a40 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8682.14% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.6 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8687.69% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u53cc\u6307\u9488\uff0c\u5355\u6b21\u5faa\u73af\n    [1,2,3,4,5]n = 2\n\n       i      j\n    record -&gt; 1 -&gt; 2 -&gt; 3 -&gt; 4 -&gt; 5 -&gt; None         # \u53cc\u6307\u9488\n\n       i                j\n    record -&gt; 1 -&gt; 2 -&gt; 3 -&gt; 4 -&gt; 5 -&gt; None         # \u5148\u79fb\u52a8j\u6307\u9488\uff0cn\u6b65\n\n                        i              j\n    record -&gt; 1 -&gt; 2 -&gt; 3 -&gt; 4 -&gt; 5 -&gt; None         # \u7136\u540e\u4e00\u8d77\u79fb\u52a8i, j\u6307\u9488\uff0c\u76f4\u5230j\u4e3aNone\n\n                        i              j\n    record -&gt; 1 -&gt; 2 -&gt; 3 -&gt; 4 -&gt; 5 -&gt; None         # \u6b64\u65f6i\u6307\u9488\u6307\u5411\u7684\u4e0b\u4e00\u4e2a\u5143\u7d20\u5373\u4e3a\u9700\u8981\u5220\u9664\u7684\u5143\u7d20\n                        |---------|\n\"\"\"\nclass Solution:\n    def removeNthFromEnd(self, head: ListNode, n: int) -&gt; ListNode:\n        record = ListNode(0)\n        record.next = head\n        i, j = record, head\n\n        for _ in range(n):\n            j = j.next\n\n        while j:\n            i = i.next\n            j = j.next\n        i.next = i.next.next\n\n        return record.next<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u94fe\u8868\uff0c\u5220\u9664\u94fe\u8868\u7684\u5012\u6570\u7b2c&nbsp;n&nbsp;\u4e2a\u8282\u70b9\uff0c\u5e76\u4e14\u8fd4\u56de\u94fe\u8868\u7684\u5934\u7ed3\u70b9\u3002 \u793a\u4f8b\uff1a \u7ed9\u5b9a\u4e00\u4e2a\u94fe\u8868:&#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\/635"}],"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=635"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/635\/revisions"}],"predecessor-version":[{"id":636,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/635\/revisions\/636"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=635"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=635"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=635"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}