C Solution


  • 0
    L
    struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) {
        struct ListNode* l3 = NULL;
        struct ListNode* rear = NULL;
        if (!l1)
            return l2;
        if(!l2)
            return l1;
        while(l1&&l2)
        {
            if(l1->val<l2->val)
            {
                if(!l3)
                {
                    l3=rear=l1;
                    l1=l1->next;
                }
                else
                {
                    rear->next = l1;
                    rear = l1;
                    l1=l1->next;
                }
            }
            else
            {
                if(!l3)
                {
                    l3=rear=l2;
                    l2=l2->next;
                }
                else
                {
                    rear->next = l2;
                    rear = l2;
                    l2=l2->next;
                }
            }
        }
        
        rear->next = (l1 == NULL?l2:l1);
        return l3;
    }
    

Log in to reply
 

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