Merge Two Sorted Lists

    就是合并两个有序链表了,递归解妥妥儿的。

ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) 
{
    if (l1 == NULL) return l2;
    if (l2 == NULL) return l1;
    
    ListNode *ret = NULL;
    
    if (l1->val < l2->val)
    {
        ret = l1;
        ret->next = mergeTwoLists(l1->next, l2);
    }
    else
    {
        ret = l2;
        ret->next = mergeTwoLists(l1, l2->next);
    }
    
    return ret;
}
posted @ 2012-09-27 11:14  紫红的泪  阅读(3524)  评论(1编辑  收藏  举报