{"id":244,"date":"2024-12-02T17:42:36","date_gmt":"2024-12-02T09:42:36","guid":{"rendered":"http:\/\/getzs.online\/?p=244"},"modified":"2024-12-02T17:42:36","modified_gmt":"2024-12-02T09:42:36","slug":"leetcode-hot100-8","status":"publish","type":"post","link":"https:\/\/hello.getzs.online\/index.php\/2024\/12\/02\/leetcode-hot100-8\/","title":{"rendered":"LeetCode Hot100 -8"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode.cn\/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>\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\"><img loading=\"lazy\" decoding=\"async\" width=\"542\" height=\"222\" src=\"http:\/\/hello.getzs.online\/wp-content\/uploads\/2024\/12\/remove_ex1.jpg\" alt=\"\" class=\"wp-image-245\" srcset=\"https:\/\/hello.getzs.online\/wp-content\/uploads\/2024\/12\/remove_ex1.jpg 542w, https:\/\/hello.getzs.online\/wp-content\/uploads\/2024\/12\/remove_ex1-300x123.jpg 300w\" sizes=\"auto, (max-width: 542px) 100vw, 542px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>head = [1,2,3,4,5], n = 2<br><strong>\u8f93\u51fa\uff1a<\/strong>[1,2,3,5]<\/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<br><strong>\u8f93\u51fa\uff1a<\/strong>[]<\/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<br><strong>\u8f93\u51fa\uff1a<\/strong>[1]<\/pre>\n\n\n\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u94fe\u8868\u4e2d\u7ed3\u70b9\u7684\u6570\u76ee\u4e3a\u00a0<code>sz<\/code><\/li>\n\n\n\n<li><code>1 &lt;= sz &lt;= 30<\/code><\/li>\n\n\n\n<li><code>0 &lt;= Node.val &lt;= 100<\/code><\/li>\n\n\n\n<li><code>1 &lt;= n &lt;= sz<\/code><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u6a21\u62df\uff1a<\/h2>\n\n\n\n<p>\u901a\u8fc7\u8ba1\u7b97\u94fe\u8868\u957f\u5ea6\uff0c\u9009\u53d6\u5012\u6570\u7b2cn\u4e2a\u6570\u8df3\u8fc7\u3002\u590d\u6742\u5ea6O(n)\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    public ListNode removeNthFromEnd(ListNode head, int n) {\n        ListNode root = new ListNode(0, head);\n        int length = 0;\n        while (head != null) {\n            length++;\n            head = head.next;\n        }\n        ListNode cur = root;\n        for (int i = 1 ; i &lt;= length - n ; i++) {\n            cur = cur.next;\n        }\n        cur.next = cur.next.next;\n        ListNode ans = root.next;\n        return ans;\n    }<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u6808\u8fd0\u7528\uff1a<\/h2>\n\n\n\n<p>\u901a\u8fc7\u6808\u539f\u7406\uff0c\u9006\u5e8f\u83b7\u5f97\u5012\u6570\u7b2cn\u4e2a\u6570\u3002\u590d\u6742\u5ea6O(n)\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    public ListNode removeNthFromEnd(ListNode head, int n) {\n        ListNode root = new ListNode(0, head);\n        head = root;\n        LinkedList&lt;ListNode> list = new LinkedList&lt;>();\n        while (root != null) {\n            list.add(root);\n            root = root.next;\n        }\n\n        ListNode node = null;\n        while (n > 1) {\n            node = list.removeLast();\n            n--;\n        }\n        list.removeLast();\n        list.getLast().next = node;\n        return head.next;\n    }<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u53cc\u6307\u9488\uff1a<\/h2>\n\n\n\n<p>\u901a\u8fc7\u5feb\u6162\u6307\u9488\uff0c\u8ba9l1\u548cl2\u5deen\u4e2a\uff0c\u5f53l2\u4e3anull\u65f6l1\u5230\u8fbe\u5012\u6570\u7b2cn\u4e2a\u6570\u3002\u590d\u6742\u5ea6O(n)\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    public ListNode removeNthFromEnd(ListNode head, int n) {\n        ListNode l1 = head, l2 = head;\n        int k = n;\n        while (k-- > 0) {\n            l2 = l2.next;\n        }\n        if (l2 == null) {\n            return head.next;\n        }\n        while (l2.next!= null) {\n            l1 = l1.next;\n            l2 = l2.next;\n        }\n        l1.next = l1.next.next;\n        return head;\n    }<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>19. \u5220\u9664\u94fe\u8868\u7684\u5012\u6570\u7b2c N \u4e2a\u7ed3\u70b9 \u7ed9\u4f60\u4e00\u4e2a\u94fe\u8868\uff0c\u5220\u9664\u94fe\u8868\u7684\u5012\u6570\u7b2c&nbsp;n&nbsp;\u4e2a\u7ed3\u70b9\uff0c\u5e76\u4e14\u8fd4\u56de [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[3],"class_list":["post-244","post","type-post","status-publish","format-standard","hentry","category-leetcode","tag-leetcode-hot100"],"_links":{"self":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/244","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/comments?post=244"}],"version-history":[{"count":1,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/244\/revisions"}],"predecessor-version":[{"id":246,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/244\/revisions\/246"}],"wp:attachment":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/media?parent=244"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/categories?post=244"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/tags?post=244"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}