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

合并两个有序的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. Ajax请求访问action推断文件是否存在

    action措辞: public ActionForward fileIsExsit(ActionMapping mapping, ActionForm form, HttpServletReques ...

  2. poj 3975&amp;&amp;hdu 1850 (nim)

    //赢得了上风 //从n几年移除堆叠一堆石头,有多少可取的石头堆 # include <stdio.h> # include <string.h> # include < ...

  3. cocos2d 简单的日常高仿酷跑游戏

    1.第一个直接看看这个游戏看起来视频(GIF我们不能满足游戏展) 跑酷游戏最纠结的是地图.碰撞倒是简单,能够自己写或者使用box2d等物理引擎.跑酷游戏地图的特点就是随机性.可是随机中又有策划特意安排 ...

  4. SQLServer表变量对IO及内存影响测试

    原文:SQLServer表变量对IO及内存影响测试 1. 测试创建表变量对IO的影响 测试创建表变量前后,tempdb的空间大小,目前使用sp_spaceused得到大小,也可以使用视图sys.dm_ ...

  5. net 试图加载格式不正确的程序。(Exception from HRESULT: 0x8007000B)

    原文:net 试图加载格式不正确的程序.(Exception from HRESULT: 0x8007000B) Server Error in '/' Application. 试图加载格式不正确的 ...

  6. Class loader:static

    package classloader; public class ClassLoaderDisplayDemo { public static void main(String[] args) { ...

  7. thoughtworks笔试整理

    笔试了,时间1个半小时.没想到居然有7/10是开放性问题.大意例如以下:1.为什么选择增加ThoughtWorks.200字以内,不能用"interesting"."ch ...

  8. Android Activity和Fragment生命周期图

  9. 编程算法 - 求1+2+...+n(函数指针) 代码(C++)

    求1+2+...+n(函数指针) 代码(C++) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 求1+2+...+n, 要求不能使用乘除法\for\whi ...

  10. 如何使用 RMAN 异构恢复一些表空间

    在oracle 在日常维护的数据库中难免会遇到误删数据和使用(drop.delete. truncate)当我们使用常规手段(flashback query .flashback drop)当数据不能 ...