My recursive algorithm(very easy and clear)


  • 0
    T
    //Created by Chen Tang
    //a very easy and clear algorithm
    
    ListNode* removeElements(ListNode* head, int val) {
            
            //base case
            if(head == NULL)
            {
                return NULL;
            }
            //recursively run the function 
            if(head->val != val)
            {
                //run the function and return the val
                head->next = removeElements(head->next, val);
                return head;
            }
            else
            {   
                //recursively run the function until the node's value is not equal to the given val
                return removeElements(head->next, val);
            }
            
            
        }
    };

Log in to reply
 

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