Merge Two Sorted Lists 解答
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.
Solution
The key to the solution here is to create a new linked list by creating a fake head.
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if (l2 == null)
return l1;
if (l1 == null)
return l2;
ListNode fakeHead = new ListNode(0);
ListNode tmp = fakeHead;
ListNode p1 = l1, p2 = l2;
while (p1 != null && p2 != null) {
if (p1.val < p2.val) {
tmp.next = p1;
p1 = p1.next;
} else {
tmp.next = p2;
p2 = p2.next; }
tmp = tmp.next;
}
while (p1 != null) {
tmp.next = p1;
tmp = tmp.next;
p1 = p1.next;
}
while (p2 != null) {
tmp.next = p2;
tmp = tmp.next;
p2 = p2.next;
}
return fakeHead.next;
}
}
Merge Two Sorted Lists 解答的更多相关文章
- Merge k Sorted Lists 解答
Question Merge k sorted linked lists and return it as one sorted list. Analyze and describe its comp ...
- [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 k Sorted Lists 解题报告
Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...
- [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 ...
- leetCode练题——21. Merge Two Sorted Lists(照搬大神做法)
1.题目 21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new l ...
- 刷题21. Merge Two Sorted Lists
一.题目说明 这个题目是21. Merge Two Sorted Lists,归并2个已排序的列表.难度是Easy! 二.我的解答 既然是简单的题目,应该一次搞定.确实1次就搞定了,但是性能太差: R ...
- 刷题23. Merge k Sorted Lists
一.题目说明 这个题目是23. Merge k Sorted Lists,归并k个有序列表生成一个列表.难度为Hard,实际上并不难,我一次提交就对了. 二.我的解答 就是k路归并,思路很简单,实现也 ...
- [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 ...
随机推荐
- jquery UI推荐
Bootstrap http://www.bootcss.com/ http://www.ligerui.com/ http://j-ui.com/#demo_page2 http://jqueryu ...
- 链表的基本操作(Basic Operations on a Linked List)
链表可以进行如下操作: 创建新链表 增加新元素 遍历链表 打印链表 下面定义了对应以上操作的基本函数. 创建新链表 新链表创建之后里面并没有任何元素,我们要为数据在内存中分配节点,再将节点插入链表.由 ...
- Hdu5785-Interesting(回文串处理)
Problem Description Alice get a string S. She thinks palindrome string is interesting. Now she wanna ...
- SSH2.0编程 ssh协议过程实现
之前为了自己做一套SSH,先自己实现了一套telnet.但经过这么多天的苦逼,发现以前的工作都是徒劳.ssh的协议很繁杂,核心的内容在于密码算法,而且自己很难在网上找到周全的细节讲解与详细的实现,只有 ...
- Activity的onSaveInstanceState()和onRestoreInstanceState()方法
首先Android的Activity生命周期如下图: Activity的onSaveInstanceState()和onRestoreInstanceState()并不是生命周期方法,他们不同于onC ...
- Appstore 创建App步骤
.进入AppInformation界面 defaultLanguage:选择默认语言 AppName:填写在Appstore上显示的名字 SKUNumber:填写一个唯一标示符,这个只要唯一即可,不能 ...
- C#设置程序自启动
public static void SetAutoRun(string fileName, bool isAutoRun) { RegistryKey reg = ...
- IOS 手机端搜索硬件设备 --- 物联网
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<errno.h> #i ...
- VC++ 控制台不自动退出
1.Ctrl+F5 2.结尾添加 getchar() 3.结尾添加 system("pause"); 参考:http://jingyan.baidu.com/article/555 ...
- K - Ignatius and the Princess IV
Description "OK, you are not too bad, em... But you can never pass the next test." ...