static int x=[](){
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return 0;
}();
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode* index1 = l1;
ListNode* index2 = l2;
if (l1 == NULL && l2 != NULL)
return l2;
if (l1 != NULL && l2 == NULL)
return l1;
ListNode* res = new ListNode(0);
ListNode* index = res;
while(index1 != NULL && index2 != NULL){
if (index1->val < index2->val)
index->next = new ListNode(index1->val), index1 = index1->next;
else
index->next = new ListNode(index2->val), index2 = index2->next;
index = index->next;
}
if (index1 != NULL)
index->next = index1;
if (index2 != NULL)
index->next = index2;
return res->next;
}
};
LetCode 21. 合并两个有序链表
最新推荐文章于 2024-08-15 22:06:50 发布