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 解答的更多相关文章

  1. Merge k Sorted Lists 解答

    Question Merge k sorted linked lists and return it as one sorted list. Analyze and describe its comp ...

  2. [LeetCode] Merge k Sorted Lists 合并k个有序链表

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

  3. LeetCode: Merge k Sorted Lists 解题报告

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

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

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

  6. 刷题21. Merge Two Sorted Lists

    一.题目说明 这个题目是21. Merge Two Sorted Lists,归并2个已排序的列表.难度是Easy! 二.我的解答 既然是简单的题目,应该一次搞定.确实1次就搞定了,但是性能太差: R ...

  7. 刷题23. Merge k Sorted Lists

    一.题目说明 这个题目是23. Merge k Sorted Lists,归并k个有序列表生成一个列表.难度为Hard,实际上并不难,我一次提交就对了. 二.我的解答 就是k路归并,思路很简单,实现也 ...

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

  9. [LintCode] Merge Two Sorted Lists 混合插入有序链表

    Merge two sorted (ascending) linked lists and return it as a new sorted list. The new sorted list sh ...

随机推荐

  1. 【HDU1233】还是畅通工程(MST基础题)

    无坑,裸题.直接敲就恩那个AC. #include <iostream> #include <cstring> #include <cstdio> #include ...

  2. 【HDU2120】Ice_cream's world I(并查集基础题)

    查环操作,裸题.一次AC. #include <iostream> #include <cstring> #include <cstdlib> #include & ...

  3. Python进阶之路---1.1python简介

                            Python简介 Python简介 Python (发音:[ 'paiθ(ə)n; (US) 'paiθɔn ]n.蟒蛇,巨蛇 ),是一种面向对象的解释 ...

  4. 推荐几个常用的jquery ui 框架

    jQuery ui框架很多,除了官方提供的jquery UI(如果你还不知道什么是jQuery UI,请看下载了jquery ui后如何使用),还有很多第三方提供的ui框架,因官方提供的jquery ...

  5. contentSize、contentInset和contentOffset

    contentSize.contentInset和contentOffset 是 scrollView三个基本的属性. contentSize: The size of the content vie ...

  6. TaobaoProtect.exe,Alipaybsm.exe进程删除----让流氓软件滚粗

    可能经常上网的朋友都会有这样的经历,只要你上过一次淘宝,那么阿里会给你的电脑自动下载一个TaobaoProtect.exe的程序,这是支付宝安全控件,名为安全控件,实际上它会在后台搜集用户数据和信息, ...

  7. javaScript表单焦点自动切换

    ---恢复内容开始--- <html> <head> <script> window.onload=function(){ var form=document.ge ...

  8. java集合--Queue用法

    队列是一种特殊的线性表,它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作.进行插入操作的端称为队尾,进行删除操作的端称为队头.队列中没有元素时,称为空队列. 在队列这 ...

  9. js渲染的3d玫瑰

    参看下面链接: 程序员最美情人节礼物:JS渲染的3D玫瑰

  10. Log4Net 的简要配置

    引用log4net.dll AssemblyInfo.cs中 [assembly: AssemblyVersion("1.0.0.0")] [assembly: AssemblyF ...