合并两个有序链表
常见的题目
/**
* 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;
else if(l2==NULL)
return l1;
ListNode* mergeList=NULL;
if(l1->val>l2->val){
mergeList=l2;//
mergeList->next=mergeTwoLists(l2->next, l1);
}else{
mergeList=l1;//
mergeList->next=mergeTwoLists(l1->next, l2);
}
return mergeList;
}
};