之前忘记记录这题了,现在补上。

合并两个有序的list,要求是:

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;
* 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 *tmp = new ListNode();
ListNode *head = tmp;
while(l1 && l2)
{
if (l1 -> val <= l2 -> val)
{
tmp -> next = l1;
tmp = l1;
l1 = l1 -> next;
}
else
{
tmp -> next = l2;
tmp = l2;
l2 = l2 -> next;
}
}
if (l1)
{
tmp -> next = l1;
}
else
{
tmp -> next = l2;
}
return head -> next;
}
};

leetcode [64] merge tow sorted lists的更多相关文章

  1. 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. 解 ...

  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. 蜗牛慢慢爬 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 ...

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

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

  5. [Leetcode Week4]Merge Two Sorted Lists

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

  6. [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 ...

  7. [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 ...

  8. 【leetcode】Merge k Sorted Lists

    Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...

  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. K60 启动过程分析

    很高兴老师借给我一K60的开发板,趁着暑假好好鼓捣鼓捣! 有了上图的过程分析我想心里大概有个低了吧! 以下看代码: /* CodeWarrior ARM Runtime Support Library ...

  2. ashx一般处理程序和HttpHandler

    asp.net项目中,使用.ashx的文件(一般处理程序)可以用于处理客户端发送来的请求,并将服务器端的处理结果返回给客户端.它能返回的类型可以是文本.或者图片.有时候,我们可以在项目中使用.cs的文 ...

  3. MYSQL C API 记录

    一.环境与条件 MySQL AB 提供了C API,能够提供低等级界面,负责完毕涉及SQLserver交互的大多数常规任务:数据库连接 .查询.结果集处理和错误处置.C API通过两个组件实现: 头文 ...

  4. poj 2689 巧妙地运用素数筛选

    称号: 给出一个区间[L,R]求在该区间内的素数最短,最长距离. (R < 2 * 10^9 , R - L <= 10 ^ 6) 由数论知识可得一个数的因子可在开根号内得到. 所以,我们 ...

  5. linux下各种文件格式的压缩以及解压缩命令

    From : http://blog.csdn.net/mu0206mu/article/details/17732857 -------------------------------------- ...

  6. bootstrap之Click大事

    上一篇文章中谈到了bootstrap流程,本文开始把目光bootstrap它可以接受指令(从源代码视图的透视.因为appium该项目现在还处于不断更新,因此,一些指令已经实现.也许未来会实现一些.从视 ...

  7. 在 Windows 7 Professional、Enterprise 或 Ultimate 上安装 IIS 7.5

    原文 在 Windows 7 Professional.Enterprise 或 Ultimate 上安装 IIS 7.5 应用到: Windows Server 2008 R2 默认情况下,Wind ...

  8. JavaScript中的分号插入机制

    原文:JavaScript中的分号插入机制 仅在}之前.一个或多个换行之后和程序输入的结尾被插入 也就是说你只能在一行.一个代码块和一段程序结束的地方省略分号. 也就是说你可以写如下代码 functi ...

  9. js之按键总结

    js 实现键盘记录 兼容FireFox和IE 2009-01-07 11:43 作者:羽殇仁 转载请注明出处,谢谢. 本篇文章是我的第一百篇blog文章,恭喜一下! 这两天突然想弄弄js的键盘记录,所 ...

  10. Android 4.4(KitKat)表格管理子系统 - 骨架

    原文地址:http://blog.csdn.net/jinzhuojun/article/details/37737439 窗体管理系统是Android中的主要子系统之中的一个.它涉及到App中组件的 ...