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.

Example:

Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4

list顺序合并就可以了
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode res = new ListNode(0);
ListNode l3 = res;
while (l1 != null && l2 != null) {
if (l1.val < l2.val) {
l3.next = l1;
l3 = l3.next;
l1 = l1.next;
} else {
l3.next = l2;
l3 = l3.next;
l2 = l2.next;
}
}
if (l1 == null) {
l3.next = l2;
} else {
l3.next = l1;
}
return res.next;
}
}

[LeetCode]21. Merge Two Sorted Lists合并两个有序链表的更多相关文章

  1. leetcode 21 Merge Two Sorted Lists 合并两个有序链表

    描述: 合并两个有序链表. 解决: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { if (!l1) return l2; if (!l2) ...

  2. LeetCode 21. Merge Two Sorted Lists合并两个有序链表 (C++)

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

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

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

  4. 【LeetCode】Merge Two Sorted Lists(合并两个有序链表)

    这道题是LeetCode里的第21道题. 题目描述: 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1-&g ...

  5. 21. Merge Two Sorted Lists(合并2个有序链表)

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

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

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

  8. 021 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 ...

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

随机推荐

  1. linux防火墙(四)—— SNET和DNET原理及应用

    图(1) 一.SNAT策略 应用环境:局域网主机共享单个公网IP地址接入Internet,简单的说就是企业内部局域网想访问外部服务器时,网关型防火墙需要开启的SNAT应用策略. SNAT策略原理:网关 ...

  2. Jenkins持续集成企业实战系列之Jenkins持续集成简介及安装-----02

    1.      Jenkins持续集成简介 注:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.    最初接触Jenkins也是由于公司需求, ...

  3. KVO - 观察自定义属性值

    1 . 声明属性&注册监听 { BOOL isOk; } [self addObserver:self forKeyPath:@"isOk" options:0 conte ...

  4. prcharm 注册码

    JetBrains全系列在线激活中心 使用方法: 1. 点击Help,选择Register.打开注册页面. 2. 选择License server, 在License server address 中 ...

  5. Chrome浏览器如何完美实现滚动截图技巧

    一.前言 我们平时在浏览网页时,想把碰到好的网页内容或者文章截屏保存,但是网页的长度常常会超出屏幕高度,一般的截屏功能只能截取显示在屏幕上的内容,那我们该如何方便快捷截取全部内容?今天就分享一个如何利 ...

  6. ProgressBar(进度条)、SeekBar(拖动条)与星级评分条(RatingBar)

    1.ProgressBar(进度条) (1)介绍 (2)常用属性 (3)xml代码 <ProgressBar android:id="@+id/progressBar2" s ...

  7. hexo 博客

    梦飞扬~ 个人网站:Mauger`s Blog 博客园 标签 新随笔 随笔 管理 Github 随笔 - 61  文章 - 1  评论 - 0 使用Node.js+Hexo+Github搭建个人博客 ...

  8. UVA - 11584 DP 最少线段覆盖

    题意:用最少的不可交线段覆盖整个区间,求该最小值 课上摸鱼的时候没注意到题意的转换,写了没啥卵用的回文中心最长枚举,所以代码里的st和h/h2是几乎没用的 注意状态转移的时候不要只用最长线段去转移,这 ...

  9. C#工具类之素数扩展类

    /// <summary> /// 素数帮忙类 /// 本类是从.net源码 类 internal static class HashHelpers 类里抽取相应的代码 /// https ...

  10. 1091 N-自守数 (15 分)

    如果某个数 K 的平方乘以 N 以后,结果的末尾几位数等于 K,那么就称这个数为“N-自守数”.例如 3×92​2​​=25392,而 25392 的末尾两位正好是 92,所以 92 是一个 3-自守 ...