{"id":5665,"date":"2019-09-30T23:14:56","date_gmt":"2019-10-01T06:14:56","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5665"},"modified":"2019-09-30T23:15:46","modified_gmt":"2019-10-01T06:15:46","slug":"leetcode-82-remove-duplicates-from-sorted-list-ii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/list\/leetcode-82-remove-duplicates-from-sorted-list-ii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 82. Remove Duplicates from Sorted List II"},"content":{"rendered":"\n<p>Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only&nbsp;<em>distinct<\/em>&nbsp;numbers from the original list.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> 1-&gt;2-&gt;3-&gt;3-&gt;4-&gt;4-&gt;5\n<strong>Output:<\/strong> 1-&gt;2-&gt;5\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> 1-&gt;1-&gt;1-&gt;2-&gt;3\n<strong>Output:<\/strong> 2-&gt;3<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution:<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  ListNode *deleteDuplicates(ListNode *head) {\n    if (!head) return nullptr;\n    ListNode dummy = ListNode(0);\n    ListNode *p = &dummy;\n    while (head) {\n      int c = 0;\n      while (head->next && head->next->val == head->val) {\n        head = head->next;\n        c++;\n      }\n      if (c == 0)\n        p = p->next = head;        \n      else\n        p->next = nullptr;      \n      head = head->next;\n    }\n    return dummy.next;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Related Problems<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/list\/leetcode-83-remove-duplicates-from-sorted-list\/\">https:\/\/zxi.mytechroad.com\/blog\/list\/leetcode-83-remove-duplicates-from-sorted-list\/<\/a><\/li><li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/list\/leetcode-86-partition-list\/\">https:\/\/zxi.mytechroad.com\/blog\/list\/leetcode-86-partition-list\/<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only&nbsp;distinct&nbsp;numbers from the original list. Example 1: Input: 1-&gt;2-&gt;3-&gt;3-&gt;4-&gt;4-&gt;5 Output: 1-&gt;2-&gt;5 Example&#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":[333,83,177],"class_list":["post-5665","post","type-post","status-publish","format-standard","hentry","category-list","tag-duplicate","tag-list","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5665","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=5665"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5665\/revisions"}],"predecessor-version":[{"id":5668,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5665\/revisions\/5668"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5665"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5665"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5665"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}