/**
* 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* cur1 = list1;
ListNode* cur2 = list2;
ListNode* temp = new ListNode(0);
ListNode* res = temp;
while (cur1 && cur2) {
if (cur1 ->val <= cur2 ->val) {
temp ->next = cur1;
cur1 = cur1 ->next;
}
else {
temp ->next = cur2;
cur2 = cur2 ->next;
}
temp = temp ->next;
}
//扫尾
while (cur1) temp ->next = cur1, temp = temp ->next, cur1 = cur1 ->next;
while (cur2) temp ->next = cur2, temp= temp ->next, cur2 = cur2 ->next;
return res ->next;
}
};
标签:ListNode,21,temp,cur1,next,链表,val,cur2,LeetCode
From: https://www.cnblogs.com/hjy94wo/p/16621291.html