20 lines
452 B
C++
20 lines
452 B
C++
#include "s0203_remove_linked_list_elements.hpp"
|
|
|
|
ListNode* S0203::removeElements(ListNode* head, int val) {
|
|
ListNode dummy = ListNode(0, head);
|
|
ListNode* ptr = &dummy;
|
|
while (ptr->next != nullptr) {
|
|
if (ptr->next->val == val) {
|
|
if (ptr->next->next == nullptr) {
|
|
ptr->next = nullptr;
|
|
break;
|
|
} else {
|
|
ptr->next = ptr->next->next;
|
|
}
|
|
} else {
|
|
ptr = ptr->next;
|
|
}
|
|
}
|
|
return dummy.next;
|
|
}
|