力扣 203 移除链表元素
/**
* Definition for singly-linked list.
* 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* dummyHead = new ListNode(0);
dummyHead -> next = head;
ListNode* cur = dummyHead;
while(cur -> next != NULL){
if(cur->next->val == val ){
ListNode* tmp = cur -> next;
cur->next = cur->next->next;
delete cur -> next;
}
else{
cur = cur -> next;
}
}
head = dummyHead -> next;
delete dummyHead;
return head;
}
};