Leetcode 237.Delete Node in a Linked List
===
### Description
(easy)
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.
#### example 1:

Input: head = [1,2,6,3,4,5,6], val = 6
Output: [1,2,3,4,5]
#### example 2:
Input: head = [], val = 1
Output: []
#### Example 3:
Input: head = [7,7,7,7], val = 7
Output: []
### 想法
c在當整個list清為[]的時候,需要多一個判斷式把整個清掉。
python多設一個dummy節點在head之前,然後while下去比較,若是cur.next.val = val的話,cur.next = cur.next.next,用這個方法就可以少一些判斷式,比如說當head=[]的時候。但是這個方法為多加一個節點,多用一些空間
### 程式碼
#### c:
```c=
struct ListNode* removeElements(struct ListNode* head, int val){
struct ListNode* current=head;
if(head==NULL) return head;
while(current!=NULL){
if(current->next==NULL) break;
if(current->next->val==val){
current->next=current->next->next;
}else{
current=current->next;
}
}
if(head->val==val){
current=head;
head=head->next;
free(current);
}
return head;
}
```
Runtime: 17 ms
Memory Usage: 8.1 MB
#### python:
```python=
class Solution:
def removeElements(self, head: Optional[ListNode], val: int) -> Optional[ListNode]:
dummy = cur = ListNode(-1)
cur.next = head
while cur.next:
if cur.next.val == val:
cur.next = cur.next.next
else:
cur = cur.next
head = head.next
return dummy.next
```
Runtime: 89 ms
Memory Usage: 17.9 MB