class Solution {
public:
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
ListNode* root = new ListNode(0);
ListNode* result = root;
while(list1 != nullptr && list2 != nullptr)
{
if(list1->val > list2->val)
{
root->next = list2;
root = root->next;
list2 = list2->next;
}
else
{
root->next = list1;
root = root->next;
list1 = list1->next;
}
}
if(list1 != nullptr)
{
root->next = list1;
}
if(list2 != nullptr)
{
root->next = list2;
}
return result->next;
}
};
【LeetCode热题100道】21. 合并两个有序链表
最新推荐文章于 2024-09-02 20:21:30 发布