{"id":4057,"date":"2018-09-19T23:54:23","date_gmt":"2018-09-20T06:54:23","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4057"},"modified":"2018-09-20T00:05:28","modified_gmt":"2018-09-20T07:05:28","slug":"leetcode-19-remove-nth-node-from-end-of-list","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/list\/leetcode-19-remove-nth-node-from-end-of-list\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 19. Remove Nth Node From End of List"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Given a linked list, remove the\u00a0<em>n<\/em>-th node from the end of list and return its head.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"crayon:false\">Given linked list: <strong>1-&gt;2-&gt;3-&gt;4-&gt;5<\/strong>, and <strong><em>n<\/em> = 2<\/strong>.\r\n\r\nAfter removing the second node from the end, the linked list becomes <strong>1-&gt;2-&gt;3-&gt;5<\/strong>.\r\n<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<p>Given\u00a0<em>n<\/em>\u00a0will always be valid.<\/p>\n<p><strong>Follow up:<\/strong><\/p>\n<p>Could you do this in one pass?<\/p>\n<h1>Solution 0: Cheating! store the nodes in an array<\/h1>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\nclass Solution {\r\npublic:\r\n  ListNode *removeNthFromEnd(ListNode *head, int n) {\r\n    if (!head) return nullptr;\r\n    vector&lt;ListNode *&gt; nodes;\r\n    ListNode *cur = head;\r\n    while (cur) {\r\n      nodes.push_back(cur);\r\n      cur = cur-&gt;next;\r\n    }\r\n    if (n == nodes.size()) return head-&gt;next;\r\n    ListNode* nodes_to_remove = nodes[nodes.size()-n];\r\n    ListNode* parent = nodes[nodes.size() - n - 1];\r\n    parent-&gt;next = nodes_to_remove-&gt;next;\r\n    delete nodes_to_remove;\r\n    return head;\r\n  }\r\n};<\/pre>\n<\/div><\/div>\n<h1>Solution 1: Two passes<\/h1>\n<p>Time complexity: O(L)<\/p>\n<p>Space complexity: O(1)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\nclass Solution {\r\npublic:\r\n  ListNode* removeNthFromEnd(ListNode* head, int n) {\r\n    int l = 0;\r\n    ListNode* cur = head;\r\n    while (cur) {\r\n      ++l;\r\n      cur = cur-&gt;next;\r\n    }\r\n    if (n == l) {\r\n      ListNode* ans = head-&gt;next;\r\n      delete head;\r\n      return ans;\r\n    }    \r\n    l -= n;\r\n    cur = head;\r\n    while (--l) cur = cur-&gt;next;\r\n    ListNode* node = cur-&gt;next;\r\n    cur-&gt;next = node-&gt;next;\r\n    delete node;\r\n    return head;\r\n  }\r\n};<\/pre>\n<\/div><\/div>\n<h1><strong>Solution 2: Fast\/Slow Pointers + Dummy Head \/ Prev<\/strong><\/h1>\n<p>Fast pointer moves n steps first, and then slow pointer starts moving.<\/p>\n<p>When fast pointer reaches tail, slow pointer is n-th node from the end.<\/p>\n<p>Time complexity: O(L)<\/p>\n<p>Space complexity: O(1)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\nclass Solution {\r\npublic:\r\n  ListNode* removeNthFromEnd(ListNode* head, int n) {\r\n    ListNode* fast = head;\r\n    for (int i = 0; i &lt; n; ++i) \r\n      fast = fast-&gt;next;\r\n    ListNode dummy(0);\r\n    dummy.next = head;\r\n    ListNode* prev = &amp;dummy;\r\n    while (fast) {\r\n      fast = fast-&gt;next;\r\n      prev = prev-&gt;next;\r\n    }\r\n    ListNode* node = prev-&gt;next;\r\n    prev-&gt;next = node-&gt;next;\r\n    delete node;\r\n    return dummy.next;\r\n  }\r\n};<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Java<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:java decode:true \">\/\/ Author: Huahua\r\nclass Solution {\r\n  public ListNode removeNthFromEnd(ListNode head, int n) {\r\n    ListNode dummy = new ListNode(0);\r\n    dummy.next = head;\r\n    ListNode fast = head;\r\n    while (n-- &gt; 0) fast = fast.next;\r\n    ListNode prev = dummy;\r\n    while (fast != null) {\r\n      fast = fast.next;\r\n      prev = prev.next;\r\n    }\r\n    prev.next = prev.next.next;\r\n    return dummy.next;\r\n  }\r\n}<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:python decode:true\"># Author: Huahua\r\nclass Solution:\r\n  def removeNthFromEnd(self, head, n):\r\n    dummy = ListNode(0)\r\n    dummy.next = head\r\n    fast, prev = head, dummy\r\n    for _ in range(n): \r\n      fast = fast.next\r\n    while fast:\r\n      fast, prev = fast.next, prev.next\r\n    prev.next = prev.next.next\r\n    return dummy.next<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given a linked list, remove the\u00a0n-th node from the end of list and return its head. Example: Given linked list: 1-&gt;2-&gt;3-&gt;4-&gt;5, and n =&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[50],"tags":[295,352,83,177,412],"class_list":["post-4057","post","type-post","status-publish","format-standard","hentry","category-list","tag-dummy-head","tag-fast-slow","tag-list","tag-medium","tag-node","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4057","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=4057"}],"version-history":[{"count":6,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4057\/revisions"}],"predecessor-version":[{"id":4063,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4057\/revisions\/4063"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4057"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4057"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4057"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}