{"id":1911,"date":"2022-01-12T20:37:00","date_gmt":"2022-01-12T12:37:00","guid":{"rendered":"http:\/\/139.9.1.231\/?p=1911"},"modified":"2022-01-13T21:49:09","modified_gmt":"2022-01-13T13:49:09","slug":"leetcodeday19","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/01\/12\/leetcodeday19\/","title":{"rendered":"leetcodeday19 &#8211;\u5220\u9664\u94fe\u8868\u7684\u5012\u6570\u7b2c N \u4e2a\u7ed3\u70b9"},"content":{"rendered":"\n<p class=\"has-medium-pink-color has-bright-blue-background-color has-text-color has-background\"><strong>\u603b\u7ed3\u65b9\u6cd5\uff1a\u5bf9\u4e8e\u94fe\u8868\uff0c\u4e00\u822c\u5728\u94fe\u8868\u5f00\u5934\u5148\u6dfb\u52a0\u4e00\u4e2a\u5934\u5143\u7d20\uff0c\u8be5\u5143\u7d20\u6307\u5411\u94fe\u8868\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002<\/strong><\/p>\n\n\n\n<p>\u7ed9\u4f60\u4e00\u4e2a\u94fe\u8868\uff0c\u5220\u9664\u94fe\u8868\u7684\u5012\u6570\u7b2c&nbsp;<code>n<\/code><em>&nbsp;<\/em>\u4e2a\u7ed3\u70b9\uff0c\u5e76\u4e14\u8fd4\u56de\u94fe\u8868\u7684\u5934\u7ed3\u70b9\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image is-style-default\"><img src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/10\/03\/remove_ex1.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>head = [1,2,3,4,5], n = 2\n<strong>\u8f93\u51fa\uff1a<\/strong>[1,2,3,5]\n<\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>head = [1], n = 1\n<strong>\u8f93\u51fa\uff1a<\/strong>[]\n<\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 3\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>head = [1,2], n = 1\n<strong>\u8f93\u51fa\uff1a<\/strong>[1]\n<\/pre>\n\n\n\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n\n\n\n<ul><li>\u94fe\u8868\u4e2d\u7ed3\u70b9\u7684\u6570\u76ee\u4e3a&nbsp;<code>sz<\/code><\/li><li><code>1 &lt;= sz &lt;= 30<\/code><\/li><li><code>0 &lt;= Node.val &lt;= 100<\/code><\/li><li><code>1 &lt;= n &lt;= sz<\/code><\/li><\/ul>\n\n\n\n<h2>\u4e00\u822c\u65b9\u6cd5\uff1a\u4e24\u6b21for\u5faa\u73af<\/h2>\n\n\n\n<p>\u7b2c\u4e00\u6b21\u8ba1\u7b97\u957f\u5ea6\uff0c\u7b2c\u4e8c\u6b21\u91cd\u6784\u94fe\u8868<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def removeNthFromEnd(self, head: ListNode, n: int) -&gt; ListNode:\n        def getLength(head: ListNode) -&gt; int:\n            length = 0\n            while head:\n                length += 1\n                head = head.next\n            return length\n        \n        dummy = ListNode(0, head)\n        length = getLength(head)\n        cur = dummy\n        for i in range(1, length - n + 1):\n            cur = cur.next\n        cur.next = cur.next.next\n        return dummy.next<\/code><\/pre>\n\n\n\n<p>\u6539\u8fdb\u7248\uff1a<\/p>\n\n\n\n<h4 id=\"\u65b9\u6cd5\u4e09\uff1a\u53cc\u6307\u9488\">\u53cc\u6307\u9488<\/h4>\n\n\n\n<p><strong>\u601d\u8def\u4e0e\u7b97\u6cd5<\/strong><\/p>\n\n\n\n<p>\u6211\u4eec\u4e5f\u53ef\u4ee5\u5728\u4e0d\u9884\u5904\u7406\u51fa\u94fe\u8868\u7684\u957f\u5ea6\uff0c\u4ee5\u53ca\u4f7f\u7528\u5e38\u6570\u7a7a\u95f4\u7684\u524d\u63d0\u4e0b\u89e3\u51b3\u672c\u9898\u3002\u7531\u4e8e\u6211\u4eec\u9700\u8981\u627e\u5230\u5012\u6570\u7b2c<em>n<\/em>&nbsp;\u4e2a\u8282\u70b9\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e24\u4e2a\u6307\u9488<em>first<\/em>&nbsp;\u548c&nbsp;<em>second<\/em>&nbsp;\u540c\u65f6\u5bf9\u94fe\u8868\u8fdb\u884c\u904d\u5386\uff0c\u5e76\u4e14&nbsp;<em>first<\/em>&nbsp;\u6bd4<em>second<\/em>&nbsp;\u8d85\u524d&nbsp;n\u4e2a\u8282\u70b9\u3002\u5f53<em>first<\/em>&nbsp;\u904d\u5386\u5230\u94fe\u8868\u7684\u672b\u5c3e\u65f6\uff0c<em>second<\/em>&nbsp;\u5c31\u6070\u597d\u5904\u4e8e\u5012\u6570\u7b2c&nbsp;<em>n<\/em>&nbsp;\u4e2a\u8282\u70b9\u3002<\/p>\n\n\n\n<p>\u5177\u4f53\u5730\uff0c\u521d\u59cb\u65f6&nbsp;<em>first<\/em>&nbsp;\u548c&nbsp;<em>second<\/em>&nbsp;\u5747\u6307\u5411\u5934\u8282\u70b9\u3002\u6211\u4eec\u9996\u5148\u4f7f\u7528&nbsp;<em>first<\/em>&nbsp;\u5bf9\u94fe\u8868\u8fdb\u884c\u904d\u5386\uff0c\u904d\u5386\u7684\u6b21\u6570\u4e3a&nbsp;n\u3002\u6b64\u65f6\uff0c<em>first<\/em>&nbsp;\u548c&nbsp;<em>second<\/em>&nbsp;\u4e4b\u95f4\u95f4\u9694\u4e86&nbsp;<em>n<\/em>\u22121&nbsp;\u4e2a\u8282\u70b9\uff0c\u5373&nbsp;<em>first<\/em>&nbsp;\u6bd4<em>second<\/em>&nbsp;\u8d85\u524d\u4e86&nbsp;n\u4e2a\u8282\u70b9\u3002<\/p>\n\n\n\n<p>\u5728\u8fd9\u4e4b\u540e\uff0c\u6211\u4eec\u540c\u65f6\u4f7f\u7528&nbsp;<em>first<\/em>&nbsp;\u548c<em>second<\/em>&nbsp;\u5bf9\u94fe\u8868\u8fdb\u884c\u904d\u5386\u3002\u5f53&nbsp;<em>first<\/em>&nbsp;\u904d\u5386\u5230\u94fe\u8868\u7684\u672b\u5c3e\uff08\u5373&nbsp;<em>first<\/em>&nbsp;\u4e3a\u7a7a\u6307\u9488\uff09\u65f6\uff0c<em>second<\/em>&nbsp;\u6070\u597d\u6307\u5411\u5012\u6570\u7b2c&nbsp;<em>n<\/em>&nbsp;\u4e2a\u8282\u70b9\u3002<\/p>\n\n\n\n<p>\u6839\u636e\u65b9\u6cd5\u4e00\u548c\u65b9\u6cd5\u4e8c\uff0c\u5982\u679c\u6211\u4eec\u80fd\u591f\u5f97\u5230\u7684\u662f\u5012\u6570\u7b2c&nbsp;<em>n<\/em>&nbsp;\u4e2a\u8282\u70b9\u7684\u524d\u9a71\u8282\u70b9\u800c\u4e0d\u662f\u5012\u6570\u7b2c&nbsp;<em>n<\/em>&nbsp;\u4e2a\u8282\u70b9\u7684\u8bdd\uff0c\u5220\u9664\u64cd\u4f5c\u4f1a\u66f4\u52a0\u65b9\u4fbf\u3002\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u8003\u8651\u5728\u521d\u59cb\u65f6\u5c06&nbsp;<em>second<\/em>&nbsp;\u6307\u5411\u54d1\u8282\u70b9\uff0c\u5176\u4f59\u7684\u64cd\u4f5c\u6b65\u9aa4\u4e0d\u53d8\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u5f53<em>first<\/em>&nbsp;\u904d\u5386\u5230\u94fe\u8868\u7684\u672b\u5c3e\u65f6\uff0c<em>second<\/em>&nbsp;\u7684<strong>\u4e0b\u4e00\u4e2a\u8282\u70b9<\/strong>\u5c31\u662f\u6211\u4eec\u9700\u8981\u5220\u9664\u7684\u8282\u70b9\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># @lc code=start\n# Definition for singly-linked list.\n# class ListNode:\n#     def __init__(self, val=0, next=None):\n#         self.val = val\n#         self.next = next\nclass Solution:\n    def removeNthFromEnd(self, head: ListNode, n: int) -&gt; ListNode:\n        dummy = ListNode(0, head)\n        first = head\n        second = dummy\n        for i in range(n):\n            first = first.next\n\n        while first:\n            first = first.next\n            second = second.next\n        \n        second.next = second.next.next\n        return dummy.next<\/code><\/pre>\n\n\n\n<p><strong>\u590d\u6742\u5ea6\u5206\u6790<\/strong><\/p>\n\n\n\n<ul><li>\u65f6\u95f4\u590d\u6742\u5ea6\uff1a<em>O<\/em>(<em>L<\/em>)\uff0c\u5176\u4e2d&nbsp;<em>L<\/em>&nbsp;\u662f\u94fe\u8868\u7684\u957f\u5ea6\u3002<\/li><li>\u7a7a\u95f4\u590d\u6742\u5ea6:  <em>O<\/em>(1)\u3002<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u603b\u7ed3\u65b9\u6cd5\uff1a\u5bf9\u4e8e\u94fe\u8868\uff0c\u4e00\u822c\u5728\u94fe\u8868\u5f00\u5934\u5148\u6dfb\u52a0\u4e00\u4e2a\u5934\u5143\u7d20\uff0c\u8be5\u5143\u7d20\u6307\u5411\u94fe\u8868\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002 \u7ed9\u4f60\u4e00\u4e2a\u94fe\u8868\uff0c\u5220\u9664\u94fe\u8868\u7684\u5012\u6570 &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/01\/12\/leetcodeday19\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">leetcodeday19 &#8211;\u5220\u9664\u94fe\u8868\u7684\u5012\u6570\u7b2c N \u4e2a\u7ed3\u70b9<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/1911"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=1911"}],"version-history":[{"count":14,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/1911\/revisions"}],"predecessor-version":[{"id":2028,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/1911\/revisions\/2028"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=1911"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=1911"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=1911"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}