/**
* 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) {
ListNode* dummyhead = new ListNode(0);
ListNode* cur = dummyhead;
while(list1 || list2){
if(list1 && list2){
if(list1->val < list2->val){
cur->next = list1;
list1 = list1->next;
}else{
cur->next = list2;
list2 = list2->next;
}
}else if(list1){
cur->next = list1;
list1 = list1->next;
}else if(list2){
cur->next = list2;
list2 = list2->next;
}
cur = cur->next;
}
return dummyhead->next;
}
};
合并两个有序链表
于 2024-07-24 11:08:18 首次发布