Question about how to free memory?

  • 0

    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 {
        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;
                    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.