将两个有序链表合并为一个新的有序链表。
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode res(0), *t = &res;
while(l1 && l2)
{
if(l1 -> val < l2 -> val)
{
t -> next = l1;
l1 = l1 -> next;
}
else
{
t -> next = l2;
l2 = l2 -> next;
}
t = t -> next;
}
t -> next = l1 == NULL ? l2 : l1;
return res.next;
}
};
啦啦啦。
原文:https://www.cnblogs.com/songjy11611/p/12332977.html