码迷,mamicode.com
首页 > 其他好文 > 详细

[Leetcode]-Merge Two Sorted Lists

时间:2015-07-02 15:50:31      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:leetcode   merge   sorted   

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
题目:合并两个排序后的单链表,要求合并之后也是排序好的
思路:递归合并
技术分享

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next; 
 * };
 */
struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) {
    if(NULL == l1) return l2;
    if(NULL == l2) return l1;

    struct ListNode* MergeList = NULL;

    if(l1->val < l2->val)
    {
        MergeList = l1;
        MergeList->next = mergeTwoLists(l1->next,l2);
    }

    else
    {
        MergeList = l2;
        MergeList->next = mergeTwoLists(l1,l2->next);
    }


    return MergeList;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

[Leetcode]-Merge Two Sorted Lists

标签:leetcode   merge   sorted   

原文地址:http://blog.csdn.net/xiabodan/article/details/46726115

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!