simple and easy to understand C solution


  • 0
    S
    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     struct ListNode *next;
     * };
     */
    struct ListNode* removeElements(struct ListNode* head, int val) {
        struct ListNode* head_d;
        
        struct ListNode* prev;
        prev->next = head;
        head_d = prev;
    
        if(head==NULL)
        {
            return head;
        }
    
        while(head!=NULL)
        {
            if(head->val == val)
            {
                prev->next = head->next;
                free(head);
                head = head->next;
                
            }
            else{
                prev = head;
                head=head->next;
            }
        }
        
        return head_d->next;
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.