203. Remove Linked List Elements

 avatar
user_6283553
c_cpp
a year ago
666 B
2
Indexable
Never
class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        ListNode* ptr=head;
        if(head==NULL){
            return NULL;
        }
        while(ptr->val==val){
        if(ptr->val==val){
                head=ptr->next;
                ptr=head;
                if(ptr->next==NULL || ptr==NULL){
                return NULL;        
                }
            }
            
        }
        while(ptr->next!=NULL){
            if(ptr->next->val==val){
                ptr->next=ptr->next->next;
            }
            else
                ptr=ptr->next;
        }
        return head;
    }
};