用递归就能实现了
/**
* 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 == NULL) return l2;
if(l2 == NULL) return l1;
ListNode *ans = NULL;
if(l1->val > l2->val)
{
ans = l2;
l2 = l2->next;
ans->next = mergeTwoLists(l1, l2);
}
else
{
ans = l1;
l1 = l1->next;
ans->next = mergeTwoLists(l1, l2);
}
return ans;
}
};