Pointer-to-pointer solution in C++


  • 0
    G
    class Solution {
    public:
        ListNode* removeElements(ListNode* head, int val) {
            if(!head) return head;
            for(ListNode** pre= &head; *pre; ){
                ListNode * curr = *pre;
                if(curr->val==val) {
                    *pre = curr->next;
                    free(curr);
                }
                else pre = &curr->next;
            }
            return head;
        }
    };

Log in to reply
 

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