Couldn't figure out error


  • 0
    D
      ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
        if(l1==NULL&l2==NULL)
        {
            return NULL;
        }
        else if(l2==NULL)
        {
            return l1;
        }
        else if(l1==NULL)
        {
            return l2;
        }
        else
        {
            ListNode temp(min(l1->val,l2->val));
            if(l1->val>l2->val)
            {
                temp.next=mergeTwoLists(l1,l2->next);
                return &temp;
            }
            else
            {
                temp.next=mergeTwoLists(l1->next,l2);
                return &temp;
            }
        }
    }

  • 0
    W

    Be careful of local variable, whose reference should not be returned, 'cause it will be removed outside its domain, and the reference means nothing. The variable "temp" reference will be invalid when returned.


Log in to reply
 

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