/**
 * 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* dummy=new ListNode(0, head);   //dummy
//         ListNode* curr=head;
//         ListNode* prev=dummy;
//         while(curr){
//             if(curr->val==val)
//                 prev->next=curr->next;
//             else prev=curr;
//             curr=curr->next;
//         }
//         return dummy->next;
//     }
// };

class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        if(!head)
            return nullptr;
        ListNode* curr=head;
        ListNode* prev=head;
        while(curr){
            if(curr->val==val)
                prev->next=curr->next;
            else prev=curr;
            curr=curr->next;
        }
        return head->val==val?head->next:head;
    }
};