Question about how to free memory?


  • 0
    K

    I wanna ask about the memory leak. Is my code right to free the deleted node using a pointer to the node? If not, can you give me a good way to free it? Thank you very much.:P

    class Solution {
    public:
        ListNode* deleteDuplicates(ListNode* head) {
            ListNode* temp = head;
            while (temp && temp->next){
                if (temp->val == temp->next->val){
                    ListNode* dummy = temp->next;
                    temp->next = temp->next->next;
                    delete dummy;
                }
                else
                    temp = temp->next;
            }
            return head;
        }
    };
    

Log in to reply
 

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