2026年3月8日 星期日

21. Merge Two Sorted Lists

難度: Easy

類型: Linked list,Recursion 
CPP程式下載: 21.cpp

Topic:

You are given the heads of two sorted linked lists list1 and list2.

Merge the two lists into one sorted list. The list should be made by splicing together the nodes of the first two lists.

Return the head of the merged linked list.

 

Example 1:

Input: list1 = [1,2,4], list2 = [1,3,4]
Output: [1,1,2,3,4,4]

Example 2:

Input: list1 = [], list2 = []
Output: []

Example 3:

Input: list1 = [], list2 = [0]
Output: [0]

 

Constraints:

  • The number of nodes in both lists is in the range [0, 50].
  • -100 <= Node.val <= 100
  • Both list1 and list2 are sorted in non-decreasing order.

Consideration:
Linked List combined.
1. Compare and add the smaller one. Move pointer to next. When one list is ended, add the rest of another list.
2. If both lists empty, return nullptr.

Code:
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
        //int iSize1=list1.size();
        //int iSize2=list2.size();
        //int i=0,j=0;
        ListNode* outlist=nullptr;
        ListNode* outlistHead=nullptr;
        ListNode* list1tmp=list1;
        ListNode* list2tmp=list2;
        if (list1==nullptr) {
            return list2;
        }
        else if (list2==nullptr) {
            return list1;
        }
        else
        {
            
            while (1) {
                if (list2->val<list1->val) {
                     if (outlistHead==nullptr) {
                         //cout << "list2->val=" << list2->val << "\n";
                         outlist=list2;
                         list2=list2->next;
                         outlistHead=outlist;
                     }
                     else {
                         //cout << "list2->val=" << list2->val << "\n";
                         
                         outlist->next=list2;
                         list2=list2->next;
                         outlist=outlist->next;
                     }
                }
                else {
                    if (outlistHead==nullptr) {
                        outlist=list1;
                         list1=list1->next;
                         outlistHead=outlist;
                    }
                    else {
                         outlist->next=list1;
                         list1=list1->next;
                         outlist=outlist->next;
                     }
                }

                if (list1==nullptr)
                {
                    outlist->next=list2;
                    //cout << "A:list2->val=" << list2->val << "\n";
                    return outlistHead;
                }
                else if (list2==nullptr)
                {
                    outlist->next=list1;
                    return outlistHead;
                }
            }
        }
    }
};

Result:
https://leetcode.com/problems/merge-two-sorted-lists/submissions/1831161763/
Accepted
208 / 208 testcases passed
tendchen
tendchen
submitted at Nov 16, 2025 16:45
Runtime
0ms
Beats100.00%
Memory
19.58MB
Beats26.66%