A "good taste" solution in C++11


  • 0
    Y
     ListNode* removeElements(ListNode* head, int val) {
        auto indirect = &head;
      
        while (*indirect) {
            if ((*indirect)->val == val) {
                *indirect = (*indirect)->next; 
                /* free if necessary */
            } else { /* go to the next element */
                indirect = &(*indirect)->next; 
            }
        }
            
        return head;
    }
    

Log in to reply
 

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