{"id":2092,"date":"2018-03-15T09:07:02","date_gmt":"2018-03-15T16:07:02","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2092"},"modified":"2018-03-15T09:07:11","modified_gmt":"2018-03-15T16:07:11","slug":"leetcode-203-remove-linked-list-elements","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/list\/leetcode-203-remove-linked-list-elements\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 203. Remove Linked List Elements"},"content":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u79fb\u9664\u5355\u5411\u94fe\u8868\u4e2d\u6240\u6709\u503c\u7b49\u4e8eval\u7684\u8282\u70b9\u3002<\/p>\n<p><strong>Problem:<\/strong><\/p>\n<p><a href=\"https:\/\/leetcode.com\/problems\/remove-linked-list-elements\/description\/\">https:\/\/leetcode.com\/problems\/remove-linked-list-elements\/description\/<\/a><\/p>\n<p>Remove all elements from a linked list of integers that have value\u00a0<b><i>val<\/i><\/b>.<\/p>\n<p><b>Example<\/b><br \/>\n<i><b>Given:<\/b><\/i>\u00a01 &#8211;&gt; 2 &#8211;&gt; 6 &#8211;&gt; 3 &#8211;&gt; 4 &#8211;&gt; 5 &#8211;&gt; 6,\u00a0<b><i>val<\/i><\/b>\u00a0= 6<br \/>\n<i><b>Return:<\/b><\/i>\u00a01 &#8211;&gt; 2 &#8211;&gt; 3 &#8211;&gt; 4 &#8211;&gt; 5<\/p>\n<p><strong>Idea:<\/strong><\/p>\n<p>Use a dummy head<\/p>\n<p><strong>Solution:<\/strong><\/p>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(1)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 30 ms\r\nclass Solution {\r\npublic:\r\n  ListNode* removeElements(ListNode* head, int val) {\r\n    ListNode dummy{0};\r\n    dummy.next = head;\r\n    ListNode* curr = &amp;dummy;\r\n    while (curr) {\r\n      ListNode* next = curr-&gt;next;\r\n      while (next &amp;&amp; next-&gt;val == val) next = next-&gt;next;\r\n      curr-&gt;next = next;\r\n      curr = next;\r\n    }\r\n    return dummy.next;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u79fb\u9664\u5355\u5411\u94fe\u8868\u4e2d\u6240\u6709\u503c\u7b49\u4e8eval\u7684\u8282\u70b9\u3002 Problem: https:\/\/leetcode.com\/problems\/remove-linked-list-elements\/description\/ Remove all elements from a linked list of integers that have value\u00a0val. Example Given:\u00a01 &#8211;&gt; 2 &#8211;&gt; 6 &#8211;&gt; 3 &#8211;&gt; 4&#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":[222,83,252],"class_list":["post-2092","post","type-post","status-publish","format-standard","hentry","category-list","tag-easy","tag-list","tag-remove","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2092","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=2092"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2092\/revisions"}],"predecessor-version":[{"id":2094,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2092\/revisions\/2094"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2092"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2092"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2092"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}