-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path203.cpp
More file actions
28 lines (28 loc) · 711 Bytes
/
203.cpp
File metadata and controls
28 lines (28 loc) · 711 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
ListNode helper(val);
helper.next = head;
ListNode* prev = &helper;
ListNode* current = head;
while(current != NULL){
if(current->val == val){
prev->next = current->next;
delete current;
current = current->next;
}else{
prev = current;
current = current->next;
}
}
return helper.next;
}
};