My c++ sulotion


  • 0
    C

    class Solution {

    public:
    ListNode* removeElements(ListNode* head, int val) {
        if (NULL == head) return NULL;
        ListNode DummyNode(0);
        DummyNode.next = head;
        ListNode **pNext = &(DummyNode.next);
        
        while (*pNext!=NULL) {
            if ((*pNext)->val==val) {
                *pNext = (*pNext)->next;
            } else {
                pNext = &((*pNext)->next);
            }
        }
        
        return DummyNode.next;
    }
    

    };


  • 0
    N
     ListNode* removeElements(ListNode* head, int val) {
        
        
        if(head == NULL){
            return NULL;
        }
        
        if(head->val == val){
            head =  removeElements(head->next, val);
        }else{
            head->next = removeElements(head->next, val);
        }
        
        return head;
    

Log in to reply
 

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