203. Remove Linked List Elements

 avatar
user_6283553
c_cpp
2 years ago
666 B
4
Indexable
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;
    }
};
Editor is loading...