题目:

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) {
ListNode dummy(-);
ListNode *p = &dummy;
while ( l1 && l2 )
{
if ( l1->val<l2->val )
{
p->next = l1;
l1 = l1->next;
}
else
{
p->next = l2;
l2 = l2->next;
}
p = p->next;
}
p->next = l1 ? l1 : l2;
return dummy.next;
}
};

tips:

无。

========================================

第二次过这道题,l1或l2为空之后,不用一个一个链接了,可以直接补上剩下的linked list。

/**
* 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 head();
ListNode* p = &head;
while ( l1 && l2 )
{
if ( l1->val < l2->val )
{
p->next = l1;
l1 = l1->next;
}
else
{
p->next = l2;
l2 = l2->next;
}
p = p->next;
}
p->next = l1 ? l1 : l2;
return head.next;
}
};

【Merge Two Sorted Lists】cpp的更多相关文章

  1. 【Merge K Sorted Lists】cpp

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

  2. leetcode 【 Merge Two Sorted Lists 】 python 实现

    题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...

  3. leetcode 【 Merge k Sorted Lists 】python 实现

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

  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. 21. Merge Two Sorted Lists【easy】

    21. Merge Two Sorted Lists[easy] Merge two sorted linked lists and return it as a new list. The new ...

  6. 【leetcode】Merge k Sorted Lists

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

  7. 【LeetCode练习题】Merge k Sorted Lists

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

  8. 【LeetCode】【数组归并】Merge k Sorted Lists

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

  9. 【LeetCode】21. Merge Two Sorted Lists 合并两个有序链表

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,有序链表,递归,迭代,题解,leetcode, 力 ...

随机推荐

  1. IPHONE 字体加粗

    UIFONT 没有字体加精参数,但可以修发,字体名,达到加粗的效果正常:Helvetica加粗"Helvetica-Bold""TimesNewRomanPS-BoldM ...

  2. Android版年年有鱼游戏源码

    具体的功能如下: 欢迎动画界面 ,游戏运行后首先进入的界面. 菜单的选择,游戏中还需要设计出菜单界面,菜单界面是玩家对游戏的第一印象,菜单界面的美观与否,直接决定着游戏的成功与失败. 游戏的音效,游戏 ...

  3. mouseover,mouseout,mouseenter,mouseleave的区别

    1.前言 今天下午参加一个面试,对方问我写不写博客,这时候才猛然意识到好久没写东西了.最近一直在外边实习,每天有很多经历和挑战,但是却没有及时地记录下来,这一点必须得批评自己,以后得经常把自己遇到的问 ...

  4. C#实现图书馆程序导入ISO-2709格式(MARC)功能

    1.导入 /// <summary> /// 导入ISO2709 /// </summary> /// <param name="sender"> ...

  5. jQuery在HTML文档加载完毕后自动执行某个事件;

    原来onchange=“fucntionname(parms)”: <select name="country" id="selCountries_{$sn}&qu ...

  6. ASP.NET MVC5学习笔记之Action参数模型绑定基本过程

    当我们在Controller中定义一个Action,通常会定义一个或多个参数,每个参数称为一个模型,ASP.NET MVC框架提供了一种机制称为模型绑定,会尝试自动从请求的信息中实例化每一个模型并赋值 ...

  7. @Styles.Render

    1.@Styles.Render 在页面上可以用@Styles.Render("~/Content/css") 来加载css 首先要在App_Start 里面BundleConfi ...

  8. ruby cookbook

    11.2 listing object's method Oject.methods/singleton_methods/instance_methods 指定类名定义的方法在 singleton_m ...

  9. web开发中,前端javascript代码的组织结构

    网页包含三个层次: 结构(HTML) 表现(CSS) 行为(javascript) web标准中,三者要分离,网页源代码由三部分组成:.html文件..css文件和.js文件.就是说html文件中不应 ...

  10. Microsoft Power BI Designer

    1/25/2015年1月25发布的预览版本,可以通过以下地址下载,注意有x64 和x32 版本区别(和上次PowerMap一样,一般也推荐的使用x64版本) http://www.microsoft. ...