LeetCode_21. Merge Two Sorted Lists
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 together the nodes of the first two lists.
Example:
Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4
package leetcode.easy; /**
* Definition for singly-linked list. public class ListNode { int val; ListNode
* next; ListNode(int x) { val = x; } }
*/
class ListNode {
int val;
ListNode next; ListNode(int x) {
val = x;
}
} public class MergeTwoSortedLists {
@org.junit.Test
public void test() {
ListNode l11 = new ListNode(1);
ListNode l12 = new ListNode(2);
ListNode l13 = new ListNode(4);
l11.next = l12;
l12.next = l13;
l13.next = null;
print(l11); ListNode l21 = new ListNode(1);
ListNode l22 = new ListNode(3);
ListNode l23 = new ListNode(4);
l21.next = l22;
l22.next = l23;
print(l21); ListNode l = mergeTwoLists(l11, l21);
print(l);
} public static void print(ListNode l) {
while (l != null) {
System.out.print(l.val);
if (l.next != null) {
System.out.print("->");
}
l = l.next;
}
System.out.println();
} public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode head = new ListNode(0);
ListNode p = head; while (l1 != null && l2 != null) {
if (l1.val <= l2.val) {
p.next = l1;
l1 = l1.next;
} else {
p.next = l2;
l2 = l2.next;
}
p = p.next;
} if (l1 != null) {
p.next = l1;
}
if (l2 != null) {
p.next = l2;
} return head.next;
}
}
LeetCode_21. Merge Two Sorted Lists的更多相关文章
- [LeetCode] Merge k Sorted Lists 合并k个有序链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 这 ...
- [LeetCode] 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 ...
- [LintCode] Merge Two Sorted Lists 混合插入有序链表
Merge two sorted (ascending) linked lists and return it as a new sorted list. The new sorted list sh ...
- No.023:Merge k Sorted Lists
问题: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexit ...
- Merge k Sorted Lists
1. Merge Two Sorted Lists 我们先来看这个 问题: Merge two sorted linked lists and return it as a new list. The ...
- 71. Merge k Sorted Lists
Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...
- 【leetcode】Merge k Sorted Lists
Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...
- Merge Two Sorted Lists
Merge Two Sorted Lists https://leetcode.com/problems/merge-two-sorted-lists/ Merge two sorted linked ...
- 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. 解 ...
随机推荐
- Jquery Ajax跨域访问
一.同源策略 二.跨域的集中方法: 1.服务器端发送请求,服务器作为中继代理(此方法不理解) 2.iframe 3.script标签 通过动过动态生成script标签,并将src指向目标源的方式(im ...
- Mysql-Percona mysql5.7简单安装
Mysql-Percona mysql5.7简单安装 一.什么是Percona 单从mysql的角度来讲,可以把Percona理解为mysql的一个分支,因为mysql的源码是开源的,Percona就 ...
- JavaScript 常用Object
对象 - Map 功能:Map 对象保存键值对. 方法: new Map():新建一个 Map 对象 Map.prototype.has(key):返回布尔值.表示 Map 实例是否包含键对应的值. ...
- python - django (cookie)
# """ Cookile: # 因为 HTTP 请求是没有状态的,每一次请求都是独立的 Cookile 的存储: # 保存在浏览器上的 键值对. # 服务器控制着响应, ...
- python - redis 的使用
1.redis连接 redis提供两个类Redis和StrictRedis用于实现Redis的命令,StrictRedis用于实现大部分官方的命令,并使用官方的语法和命令,Redis是StrictRe ...
- echo 显示命令
echo 显示命令 echo 是在PHP里面最常用的一个输出.显示功能的命令.直线电机滑台 我们可以让他显示任何可见的字符. <?php echo 123; ?> <?php $ip ...
- YARN构建--解决cypress下载慢问题
背景 注意: 此方案仅适合已经自行搭建私有仓库的用户使用 如非必要,尽可能使用软件开发云或其他服务提供的镜像站,避免此类特殊处理(会导致仓库维护成本增加) 场景描述 YARN构 ...
- 利用webuploader实现超大文件分片上传、断点续传
之前仿造uploadify写了一个HTML5版的文件上传插件,没看过的朋友可以点此先看一下~得到了不少朋友的好评,我自己也用在了项目中,不论是用户头像上传,还是各种媒体文件的上传,以及各种个性的业务需 ...
- MongoDB存储数据
想要深入了解MongoDB如何存储数据之前,有一个概念必须清楚,那就是Memeory-Mapped Files. Memeory-Mapped Files 下图展示了数据库是如何跟底层系统打交 ...
- P1608 路径统计
题目描述 “RP餐厅”的员工素质就是不一般,在齐刷刷的算出同一个电话号码之后,就准备让HZH,TZY去送快餐了,他们将自己居住的城市画了一张地图,已知在他们的地图上,有N个地方,而且他们目前处在标注为 ...