【leetcode】10 mergetwosorted list

合并两个有序链表

常见的题目

/**
 * 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;
    }
};

posted @ 2015-05-11 14:38  自信乐观  阅读(116)  评论(0编辑  收藏  举报