c++
/**
* 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) {
if (l1 == nullptr) return l2;
if (l2 == nullptr) return l1;
ListNode *pre = new ListNode(-1);
ListNode *k = pre;
while (l1!=nullptr && l2!=nullptr){
if (l1->val <= l2->val) {
k->next = l1;
l1 = l1->next;
}
else {
k->next = l2;
l2 = l2->next;
}
k = k->next;
}
if (l1 != nullptr) k->next = l1;
if (l2 != nullptr) k->next = l2;
return pre->next;
}
};