题目:

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. AutoCAD DxfCode组码值类型

    0-9 字符串(随着从 AutoCAD 2000 起引入了扩展符号名称,字数限制已由 255 个字符扩大到 2049 个单字节字符,不包括行末的换行符) 10-39 双精度三维点值 40-59 双精度 ...

  2. 埃氏筛法(快速筛选n以内素数的个数)

    给你一个数n,请问n以内有多少个素数?(n <= 10e7) 一般来说,要是对一个整数进行素数判断,首先想到的是写个函数判断是否为素数,然后调用这个函数,时间复杂度为O(n^(½)),但是要求n ...

  3. hdoj 5389 Zero Escape

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5389 大体题意是:有两个门A和B,还有一群人,每个人都有一个数字, 疯了一样的T..比赛的时候十连T也 ...

  4. ActiveMQ集成到Spring

    [http://wentao365.iteye.com/blog/1560934] spring配置文件applicationContext.xml <?xml version="1. ...

  5. MES取所有部门的函数实例

    USE [ChangHong]GO/****** Object: UserDefinedFunction [dbo].[FN_GetDeptCode] Script Date: 04/26/2016 ...

  6. 基于FlashPaper的文档播放器

    本文主要讨论.描述了使用Adobe公司的Flex与FlashPaper产品完成对发布到网上的文档资料进行只读控制,也就是说只允许浏览操作.对下载.打印进行控制. FlashPaper FlashPap ...

  7. uva331 - Mapping the Swaps

    Mapping the Swaps Sorting an array can be done by swapping certain pairs of adjacent entries in the ...

  8. 把自定义类实例存储到LSO

    使用flash.net.registerClassAlias( )方法保留类型信息并把类实例添加到共享对象的data属性上. LSOs 使用特殊的二进制格式,Action Message Format ...

  9. Cocos2dx Widget button透明区域过滤

    小伟哥 遇到一个命题: button透明区域过滤.当点击一个建筑button.花的时候不得不想一些方法把点击透明区域过滤掉. 让点击也没有效果滴啦. 開始搜索了半天才有所思路. 在网络上非常多贴代码的 ...

  10. ASP.NET过滤HTML标签只保留换行与空格的方法

    这篇文章主要介绍了ASP.NET过滤HTML标签只保留换行与空格的方法,包含网上常见的方法以及对此方法的改进,具有一定的参考借鉴价值,需要的朋友可以参考下   本文实例讲述了ASP.NET过滤HTML ...