{"id":1893,"date":"2016-03-22T10:35:24","date_gmt":"2016-03-22T02:35:24","guid":{"rendered":"http:\/\/boweihe.me\/?p=1893"},"modified":"2016-03-22T10:35:24","modified_gmt":"2016-03-22T02:35:24","slug":"leetcode-203-remove-linked-list-elements","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=1893","title":{"rendered":"LeetCode 203. Remove Linked List Elements"},"content":{"rendered":"<p>Remove all elements from a linked list of integers that have value <b><i>val<\/i><\/b>.<br \/>\n<b>Example<\/b><br \/>\n<i><b>Given:<\/b><\/i> 1 &#8211;&gt; 2 &#8211;&gt; 6 &#8211;&gt; 3 &#8211;&gt; 4 &#8211;&gt; 5 &#8211;&gt; 6, <b><i>val<\/i><\/b> = 6<br \/>\n<i><b>Return:<\/b><\/i> 1 &#8211;&gt; 2 &#8211;&gt; 3 &#8211;&gt; 4 &#8211;&gt; 5<\/p>\n<h3>\u601d\u8def\uff1a<\/h3>\n<p>\u601d\u8def\u5f88\u7b80\u5355\uff0c\u8bb0\u5f55\u5f53\u524d\u6307\u9488\u7684\u524d\u4e00\u4e2a\u8282\u70b9\uff0c\u7136\u540eval\u5bf9\u4e0a\u7684\u8bdd\u628a\u524d\u4e00\u4e2a\u8282\u70b9\u7684next\u6307\u9488\u6307\u5411\u5f53\u524d\u8282\u70b9\u7684next\u6307\u9488\u3002\u6709\u51e0\u4e2a\u9700\u8981\u6ce8\u610f\u7684\u8f93\u5165\uff1a<\/p>\n<ol>\n<li>\u7a7a\u6307\u9488\u8f93\u5165\uff0c\u5373head\u5c31\u662fNULL<\/li>\n<li>head-&gt;val\u7b49\u4e8eval\u7684\u60c5\u51b5\uff0c\u5373\u5220\u9664\u5b8c\u540ehead\u6307\u9488\u4f1a\u53d8\u5316<\/li>\n<li>\u672b\u5c3e\u8282\u70b9\u8981\u5220\u9664\u7684\u60c5\u51b5<\/li>\n<li>\u5220\u5b8c\u4e4b\u540e\u94fe\u8868\u53d8\u7a7a\u7684\u60c5\u51b5<\/li>\n<\/ol>\n<p><em>\u300a\u5251\u6307Offer\u300b\u8fd9\u4e66\u8fd8\u633a\u6709\u7528\u7684\uff0c\u4ee3\u7801\u7684\u7a33\u5065\u6027\u5f88\u91cd\u8981\u3002<\/em><\/p>\n<h3>\u4ee3\u7801<\/h3>\n<pre class=\"lang:c++ decode:true \">\/**\n * Definition for singly-linked list.\n * struct ListNode {\n *     int val;\n *     ListNode *next;\n *     ListNode(int x) : val(x), next(NULL) {}\n * };\n *\/\nclass Solution {\npublic:\n    ListNode* removeElements(ListNode* head, int val) {\n        if(head == NULL)\n            return head;\n        ListNode* pNewHead = head;\n        ListNode* pPrev = NULL;\n        ListNode* pCurr = head;\n        while(pCurr != NULL){\n            if(pCurr-&gt;val == val){\n                if(pPrev == NULL) {\/\/pCurr is the head of linkedlist\n                    pNewHead = pCurr-&gt;next;\n                } else {\n                    pPrev-&gt;next = pCurr-&gt;next;\n                }\n            } else {\n                pPrev = pCurr;\n            }\n            pCurr = pCurr-&gt;next;\n        }\n        return pNewHead;\n    }\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Remove all elements from a linked list of integers that have value val. Example Given: 1 &#8211;&gt; 2 &#8211;&gt; 6 &#8211;&gt; 3 &#8211;&gt; 4 &#8211;&gt; 5 &#8211;&gt; 6, val = 6 Return: 1 &#8211;&gt; 2 &#8211;&gt; 3 &#8211;&gt; 4 &#8211;&gt; 5 \u601d\u8def\uff1a \u601d\u8def\u5f88\u7b80\u5355\uff0c\u8bb0\u5f55\u5f53\u524d\u6307\u9488\u7684\u524d\u4e00\u4e2a\u8282\u70b9\uff0c\u7136\u540eval\u5bf9\u4e0a\u7684\u8bdd\u628a\u524d\u4e00\u4e2a\u8282\u70b9\u7684next\u6307\u9488\u6307\u5411\u5f53\u524d\u8282\u70b9\u7684next\u6307\u9488\u3002\u6709\u51e0\u4e2a\u9700\u8981\u6ce8\u610f\u7684\u8f93\u5165\uff1a \u7a7a\u6307\u9488\u8f93\u5165\uff0c\u5373head\u5c31\u662fNULL head-&gt;val\u7b49\u4e8eval\u7684\u60c5\u51b5\uff0c\u5373\u5220\u9664\u5b8c\u540ehead\u6307\u9488\u4f1a\u53d8\u5316 \u672b\u5c3e\u8282\u70b9\u8981\u5220\u9664\u7684\u60c5\u51b5 \u5220\u5b8c\u4e4b\u540e\u94fe\u8868\u53d8\u7a7a\u7684\u60c5\u51b5 \u300a\u5251\u6307Offer\u300b\u8fd9\u4e66\u8fd8\u633a\u6709\u7528\u7684\uff0c\u4ee3\u7801\u7684\u7a33\u5065\u6027\u5f88\u91cd\u8981\u3002 \u4ee3\u7801 \/** * Definition for singly-linked list. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[66],"class_list":["post-1893","post","type-post","status-publish","format-standard","hentry","category-technical","tag-leetcode-oj"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/1893","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1893"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/1893\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}