LeetCode -- Merge Two Sorted Linked List
Question:
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.
Analysis:
1)两个都是空串;
2)一个是空串;
3)新建一个伪头结点,用于新串的连接;
4)最后还要检查是否有一个串还有数字。
Answer:
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode p1 = l1;
ListNode p2 = l2;
if(l1 == null && l2 == null)
return null;
if(l1 == null && l2 != null)
return l2;
if(l1 != null && l2 == null)
return l1;
ListNode fakeHead = new ListNode(0);
ListNode p = fakeHead;
while (p1 != null && p2 != null){
if (p1.val <= p2.val) {
p.next = p1;
p1 = p1.next;
p = p.next;
} else {
p.next = p2;
p2 = p2.next;
p = p.next;
}
}
if(p1 != null)
p.next = p1;
if(p2 != null)
p.next = p2;
return fakeHead.next;
}
LeetCode -- Merge Two Sorted Linked List的更多相关文章
- [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 ...
- LeetCode:Merge k Sorted Lists
题目链接 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexi ...
- LeetCode——Merge k Sorted Lists
Discription: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its ...
- 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 ...
- leetcode -- Merge k Sorted Lists add code
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. [ ...
- LeetCode Merge k Sorted Lists (链表)
题意 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 Lists Merge two sorted linked lists and return it as a new list. The new list shoul ...
- LeetCode: Merge k Sorted Lists 解题报告
Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...
随机推荐
- 使用Python第三方库生成二维码
本文主要介绍两个可用于生成二维码的Python第三方库:MyQR和qrcode. MyQR的使用: 安装: pip install MyQR 导入: from MyQR import myqr imp ...
- C#中的线程(二)线程同步基础 (读后感)
参考文章:https://www.cnblogs.com/dingfangbo/p/5769501.html 一.lock 确保只有一个线程访问某个资源或某段代码.通俗的讲就是多个线程操作相同的锁对象 ...
- Laravel系列之CMS系统学习 — 角色、权限配置【2】
一.RBAC分析 基于角色的权限访问控制(Role-Based Access Control),这里存在这么几个玩意儿:角色.权限,用户 表:roles.permissions.role_has_pe ...
- C++11中std::forward的使用
std::forward argument: Returns an rvalue reference to arg if arg is not an lvalue reference; If arg ...
- 第6模块 web框架口述题
状态码如200 OK,以3位数字和原因 成.数字中的 一位指定了响应 别,后两位无分 .响应 别有以下5种. 重定向:客户端像服务器端发送请求,服务器告诉客户端你去重定向(状态码302,响应头loca ...
- ActiveMQ测试实例
ActiveMQ的安装与启动 1 下载ActiveMQ:http://activemq.apache.org/download.html 2 下载后解压到任意文件夹,解压后文件夹内的目录为: 3 进入 ...
- Office Web Apps Server(1)
Office Web Apps Server runs on one or more servers and provides browser-based Office file viewi ...
- 还原T4模板执行前的警告对话框
T4模板在保存的时候都会弹出个对话框,确认是否立即执行,大部分情况下我是不想立即执行的,所以一般都点Cancel,只有想执行的时候才点OK. 今天操作的时候不小心勾选了“Do not show thi ...
- Promise关键知识
异步是ES6中使用频率最高的特性之一,也是面试中经常会被问到的问题,特此整理了相应的笔记 一.Promise的三种状态 pending-异步操作没有结束 fulfilled-异步操作已成功结束,最常见 ...
- 子串查询(二维前缀数组) 2018"百度之星"程序设计大赛 - 资格赛
子串查询 Time Limit: 3500/3000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Subm ...