# LeetCode 203. Remove Linked List Elements

Remove all elements from a linked list of integers that have value val.

Example
Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6
Return: 1 --> 2 --> 3 --> 4 --> 5

Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.

``` 1 /**
2  * Definition for singly-linked list.
3  * struct ListNode {
4  *     int val;
5  *     ListNode *next;
6  *     ListNode(int x) : val(x), next(NULL) {}
7  * };
8  */
9 class Solution {
10 public:
11     ListNode* removeElements(ListNode* head, int val) {
12         ListNode *dummy = new ListNode(-1), *pre = dummy;
14         while (pre)
15         {
16             if (pre->next && pre->next->val == val)
17             {
18                 ListNode *t = pre->next;
19                 pre->next = t->next;
20                 delete t;
21             }
22             else
23                 pre = pre->next;
24         }
25         return dummy->next;
26
27     }
28 };```

LeetCode 203. Remove Linked List Elements

(0)
(0)