题目:

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. hdu1166-敌兵布阵(线段树)

    http://acm.hdu.edu.cn/showproblem.php?pid=1166 区间更新,区间求和 // File Name: hdu1166.cpp // Author: bo_jwo ...

  2. POJ2299Ultra-QuickSort (线段树和归并排序的解法)

    题目大意就是说帮你给一些(n个)乱序的数,让你求冒泡排序需要交换数的次数(n<=500000) 此题最初真不会做,我也只是在听了章爷的讲解后才慢慢明白过来的 首先介绍线段树的解法: 我们先将原数 ...

  3. 初学VSTO一问,如何添加SheetChange事件

    很多初学者在学习VSTO时,觉得很迷茫,举一个简单的例子,在VBA中,添加SheetChange的事件,非常容易. 如下图所示,只需要在VBE界面,先把SheetChange事件就好了. 而认为在VS ...

  4. jQuery在updatepanel中使用造成内存泄露

    wijmo用户反馈了一个RadialGauge控件内存泄露的bug,采用chrome监控内存使用情况,发现明显的内存泄露,在前面的文章中我就发现了jQuery内存泄露的问题,这次再次发现此问题,自然就 ...

  5. hdoj 5399 Tpp simple

    WA了一下午.... 1WA:T了,因为阶乘没打表所以时间超了.. 2WA,3WA:runtime error,检查的value数组开小了,应该是MAXN.. 4WA.5WA.6WA:改了改对cnt的 ...

  6. Spring4.0+Hibernate4.0+Struts2.3整合包括增删改查案例,解决整合中出现的异常

    源码下载:http://download.csdn.net/detail/cmcc_1234/7034775 ======================Application.xml======== ...

  7. 用vagrant搭建一个自己的lnmp环境(一)

    用vagrant搭建自己的lnmp环境 1.工具: a.vagrant b.virtual box c.linux服务器box(此处我使用centos 7.0) 2.安装完vagrant和virtua ...

  8. box-shadow 同时有内阴影和外发光效果

    box-shadow: 0px 0px 10px rgba(0,0,0,0.8) inset,0px 0px 5px rgba(200,200,200,0.5);

  9. C# 获取字符串中的数字

    /// <summary> /// 获取字符串中的数字(不包含小数点) /// </summary> /// <param name="str"> ...

  10. JS获取URL参数 方法

    function getQueryVariable(variable) { var query = window.location.search.substring(1); var vars = qu ...