My step by step 12 ms C solution


  • 0
    C
    struct ListNode* removeElements(struct ListNode* head, int val) {
        struct ListNode *preNode = NULL, *curNode = NULL;
        while(head && head->val == val) {
            head = head->next;
        }
        if(head == NULL) {
            return head;
        }
        preNode = head;
        curNode = head->next;
        while(curNode) {
            if(curNode->val == val) {
                preNode->next = curNode->next;
                curNode = preNode->next;
            }
            else {
                preNode = curNode;
                curNode = curNode->next;
            }
        }
        return head;
    }

Log in to reply
 

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