# 203. Remove linked list elements

```/**
* 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) {}
* };
*/
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
//ListNode* temp = NULL;
ListNode* ans = NULL;
while(p != NULL){
//Now there is a problem. If you delete a node, you need to use the pointer of the previous node of the current node
//To equal the pointer in the current node
//Here is a one-way linked list. How to get the pointer of the previous node is a problem.
//Find a variable to save. The first case is special. Deal with it specially
//so what? For the first time, if two pointers move forward, then p move forward
//When p and frontP are on the same node,
//If yes, use two pointers to complete the deletion. If not, move forward at the same time
if(p == frontP){//The above judgment condition cannot be used here, because when the first is the target value,
//Then the next two pointers should move to the second. At this time, we have to make this special judgment,
//If the first one is used, no special judgment will be made, and only one judgment will be made at the beginning
//The first time it happens to be the target value, the ans has never received an assignment and is still initialized null
//Initial cases are handled separately
if(p->val == val){//*p.val, just understand and use it yourself. When using it, directly change it to - >
//temp = p;// Since the delete code below cannot be added, this is unnecessary
frontP = p->next;
p = p->next;
//delete temp;// An error will be reported with this line of code
}
else{
p = p->next;
ans = frontP;
}
continue;
}
if(p->val == val){
//temp = p;
//frontP = p->next;
frontP->next = p->next;//This is to change the original linked list. According to the above writing, the linked list will not change at all
p = p->next;
//delete temp;
}
else{
frontP = p;
p = p->next;
}
}
return ans;
}
};```

When doing this problem, first of all, it's a good habit to sort out your ideas first, and then do it. Secondly, the control of some critical situations is still a little lacking. Another problem is that delete will report an error.

There are two ways to answer this question: (standard answer on the official website)

```class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
}
}
};

Author: LeetCode-Solution
Source: force buckle( LeetCode)

This is a recursive version. When to use recursion first? You have thought about a solution to simplify the problem first, and then the simplified part can still be simplified with the same idea.

This is the special processing of the first head node, and then the subsequent deletion operation. The subsequent deletion processing can be regarded as the head node to delete in the way of special processing.

```class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
while (temp->next != NULL) {
if (temp->next->val == val) {
temp->next = temp->next->next;
} else {
temp = temp->next;
}
}
}
};

Author: LeetCode-Solution