Solution in C++ with second-level pointer


  • 0
    F

    This solution use no dummy node and no memory leaks. Use a second-level pointer

    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        ListNode* removeElements(ListNode* head, int val) {
            ListNode** p_cur  = &head;                                                                                                                                                                                                           
            while (*p_cur != NULL) {
                if ((*p_cur)->val == val) {
                    ListNode* del_node = *p_cur;
                    *p_cur = (*p_cur)->next;
                    delete del_node;
                } else {
                    p_cur = &((*p_cur)->next);
                }
            }
    
            return head;
        }   
    };
    

    Welcome to visit all the solutions on: https://github.com/fanfank/leetcode


Log in to reply
 

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