A simple C solution[Accepted]


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

Log in to reply
 

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