My C++ solution with freeing memory


  • 0
    A
    class Solution { 
    public:
    ListNode *deleteDuplicates(ListNode *head) {
        ListNode *tmpNode = head;
        
        while(tmpNode){
            if(tmpNode->next != NULL && tmpNode -> val == tmpNode -> next -> val){
                ListNode *nextNode = tmpNode -> next -> next;
                delete tmpNode -> next;//free memory
                tmpNode -> next = nextNode;
            }
            else
                tmpNode = tmpNode -> next;
        }
        
        return head;
    }
    };

Log in to reply
 

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