题目:

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.

说明:有序链表归并(从小到大)

1)此链表无头节点

实现:

方法一:非递归

 /**
* 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) {
ListNode *la=l1,*lb=l2;
ListNode *q=NULL,*p=NULL;
if(la==NULL) return l2;
if(lb==NULL) return l1;
//此单链表无头结点,若有头结点,可直接p=head节点即可,无需下面的if...else...
if((la->val) < (lb->val))
{
p=la;
la=la->next;
}
else
{
p=lb;
lb=lb->next;
}
q=p;
while(la&&lb)
{
if((la->val) < (lb->val))
{
p->next=la;
p=la;
la=la->next;
}
else
{
p->next=lb;
p=lb;
lb=lb->next;
}
}
p->next=la?la:lb;
return q;
}
};

方法二:递归(紫红的泪大牛的代码,博客链接:http://www.cnblogs.com/codingmylife/archive/2012/09/27/2705286.html

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

leetcode 题解Merge Two Sorted Lists(有序链表归并)的更多相关文章

  1. [LeetCode 题解]: Merge k Sorted Lists

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 题 ...

  2. [LeetCode] 21. Merge Two Sorted Lists 合并有序链表

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  3. Java for LeetCode 023 Merge k Sorted Lists

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解 ...

  4. [LeetCode] 21. Merge Two Sorted Lists 混合插入有序链表

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  5. [LeetCode] 23. Merge k Sorted Lists 合并k个有序链表

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. E ...

  6. LeetCode 23 Merge k Sorted Lists(合并k个有序链表)

    题目链接: https://leetcode.com/problems/merge-k-sorted-lists/?tab=Description Problem: 给出k个有序的list, 将其进行 ...

  7. 蜗牛慢慢爬 LeetCode 23. Merge k Sorted Lists [Difficulty: Hard]

    题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...

  8. [Leetcode Week4]Merge Two Sorted Lists

    Merge Two Sorted Lists题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/merge-two-sorted-lists/descrip ...

  9. Leetcode 23.Merge Two Sorted Lists Merge K Sorted Lists

    Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list shoul ...

随机推荐

  1. hdu 1176 免费馅饼(数塔类型)

    http://acm.hdu.edu.cn/showproblem.php?pid=1176 免费馅饼 Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  2. DP练习(概率,树状,状压)

    http://vjudge.net/contest/view.action?cid=51211#overview 花了好长时间了,终于把这个专题做了绝大部分了 A:HDU 3853 最简单的概率DP求 ...

  3. HDU1007最近点对(分治)

    http://acm.hdu.edu.cn/showproblem.php?pid=1007 直接见代码吧.不过这个是N*logN*logN的 尽管如此,我怎么感觉我的比他们的还快??? #inclu ...

  4. LA4329 Ping pong(树状数组与组合原理)

    N (3N20000)ping pong players live along a west-east street(consider the street as a line segment). E ...

  5. webservice接口的发布

    使用xfire-client发布webservice接口: commons-codec-1.3.jar commons-httpclient-3.0.jar 在src 下创建META-INF/xfir ...

  6. C#中的Collection 3

    IList<T> 和 ICollection<T> 最重要的一些类型 List<T>: Encapsulates[T], like array, but also ...

  7. 访问ControlTemplate内部的元素

    需要用到code behind 注意要给需要访问的元素命名x:Name="PART_TextBlock" <ResourceDictionary xmlns="ht ...

  8. JavaScript设计模式 -- 读书笔记

    JavaScript设计模式 一. 设计模式 一个模式就是一个可重用的方案: 有效的解决方法.易重用.善于表达该解决方案: 未通过"模式特性"测试的模式称为模式原型: 三规则:适用 ...

  9. ps:探索按钮按起落下的技巧

    (从死了一次又一次终于挂掉的百度空间中抢救出来的,发表日期 2014-07-10) 先上图: 那个看上去想按下去的,那个看上去像自然地呢? 显而易见: 第一像按下去的,第二个像自然地. 原因: 渐变: ...

  10. jquery 3D 标签云

    http://www.gbin1.com/technology/jquerynews/20111205tagcloudbyjquery/index.html 相关选项 zoom: 90 初始的缩放度  ...