21. Merge Two Sorted Lists

 class Solution {

public:
    ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
        if(list1 == NULL)
            return list2;
        if(list2 == NULL)
            return list1;
        
       ListNode* dummy = new ListNode();
        ListNode* temp = dummy;
        while(list1 && list2)
        {
            if(list1->val < list2->val)
            {
                temp->next = list1;
                list1 = list1->next;
            }
            else
            {
                 temp->next = list2;
                list2 = list2->next;
            }
            temp = temp->next;
        }
        
        if(list1)
            temp->next = list1;
        if(list2)
            temp->next = list2;
        return dummy->next;
    }
};

Comments

Popular posts from this blog

1431. Kids With the Greatest Number of Candies

125. Valid Palindrome

771. Jewels and Stones