An issue associated with memory free C++


  • 0
    H

    my code is this:
    class Solution {
    public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
    if(l1 == NULL) return l2;
    if(l2 == NULL) return l1;
    ListNode* l3;

     auto head = l3;
     while(l1 != NULL && l2 != NULL){
         if(l1->val <= l2->val){
             l3->next = l1;
             l3 = l1;
             l1 = l1->next;
         }
         else{
             l3->next = l2;
             l3 = l2;
             l2 = l2->next;
         }
     }
     if(l1 == NULL) l3->next = l2;
     if(l2 == NULL) l3->next = l1;
    
    // auto headtem = head;
      head = head->next;
    

    // free(headtem);
    return head;
    }
    };

    the commented two lines are to free the memory not used.
    if I uncomment these two lines, it will complain run time error. Why this happens? Thanks


Log in to reply
 

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