---
# System prepended metadata

title: 203. Remove Linked List Elements
tags: [Leetcode, list, python, c++, easy]

---

###### tags: `Leetcode` `easy` `list` `python` `c++`

# 203. Remove Linked List Elements

## [題目連結:] https://leetcode.com/problems/remove-linked-list-elements/

## 題目:
Given the head of a linked list and an integer val, remove all the nodes of the linked list that has Node.val == val, and return the new head.

![](https://i.imgur.com/e25W2Va.png)

#### [圖片來源:] https://leetcode.com/problems/remove-linked-list-elements/

## 解題想法:

如圖:
![](https://i.imgur.com/o5Rwd8l.png)

* 若cur.val不等於目標值,pre、cur均正常往前
* 若cur.val等於目標值,pre.next=cur.next ,cur往前

## Python:
``` python=
class ListNode(object):
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next
    def insert(self,node):
        if self.next==None:
            self.next=ListNode(node)
        else:
            self.next.insert(node)
    def printList(self):
        res=[]
        head=self
        while head:
            res.append(head.val)
            head=head.next
        print(res)
            
class Solution(object):
    def removeElements(self, head, val):
        """
        :type head: ListNode
        :type val: int
        :rtype: ListNode
        """
        if not head:
            return head
        dummy=ListNode(0)
        dummy.next=head
        pre=dummy
        cur=head
        while cur:
            if cur.val==val:
                pre.next=cur.next
                cur=cur.next
            else:
                pre=cur
                cur=cur.next
        return dummy.next

if __name__ == '__main__':
    head=ListNode(1)
    head.insert(2)
    head.insert(6)
    head.insert(3)
    head.insert(4)
    head.insert(5)
    head.insert(6)
    head.printList()
    
    result=Solution()
    ans=result.removeElements(head,6)
    ans.printList()
```

## C++:
``` cpp=
#include<iostream>
#include<vector>
using namespace std;

struct ListNode{
    int val;
    ListNode *next;
    ListNode(): val(0), next(nullptr) {}
    ListNode(int x): val(x), next(nullptr) {}
    ListNode(int x, ListNode *next): val(x), next(next) {}
};

void InsertNode(ListNode *head, int data){
    ListNode *tail=head;
    if (tail->next==nullptr){
        ListNode *new_node= new ListNode(data);
        tail->next=new_node;
    }
    else
        InsertNode(tail->next,data);
}

void PrintList(ListNode *head){
    vector<int> res;
    ListNode *tail=head;
    while (tail!=nullptr){
        res.push_back(tail->val);
        tail=tail->next;
    }
    for (int i=0; i<res.size(); i++){
        cout<<res[i]<<" ";
    }
    cout<<endl;
}

class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        ListNode *dummy=new ListNode(0);
        dummy->next=head;
        ListNode *pre=dummy;
        ListNode *cur=head;
        while (cur!=nullptr){
            if (cur->val==val){
                pre->next=cur->next;
                cur=cur->next;
            }
            else{
                pre=cur;
                cur=cur->next;
            }
        }
        return dummy->next;
    }
};

int main(){
    ListNode *head=new ListNode(1);
    InsertNode(head,2);
    InsertNode(head,6);
    InsertNode(head,3);
    InsertNode(head,4);
    InsertNode(head,5);
    InsertNode(head,6);
    cout<<"Original List: ";
    PrintList(head);

    Solution res;
    ListNode *ans=res.removeElements(head,6);
    cout<<"After delete: ";
    PrintList(ans);

    return 0;
}

```
