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.


  public ListNode mergeTwoLists(ListNode l1, ListNode l2) {

         ListNode p1 = l1;
ListNode p2 = l2; ListNode fakeHead = new ListNode(0);
ListNode p = fakeHead; while(p1 != null && p2 != null){
if(p1.val <= p2.val){
p.next = p1;
p1 = p1.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;
}

重点是next边界的使用,注意一点 最好使用新指针 不要改动原有的东西。

LeetCode 21 -- Merge Two Sorted Lists的更多相关文章

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

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

  3. [leetcode] 21. Merge Two Sorted Lists (Easy)

    合并链表 Runtime: 4 ms, faster than 100.00% of C++ online submissions for Merge Two Sorted Lists. class ...

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

  5. Java [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 spli ...

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

  7. Leetcode 21. Merge Two Sorted Lists(easy)

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  8. (链表) 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 ...

  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. chrome (failed) net::ERR_INCOMPLETE_CHUNKED_ENCODING ashx 加载图片

    chrome (failed) net::ERR_INCOMPLETE_CHUNKED_ENCODING   ashx文件加载图片的方法,发现在chrome浏览器里面出了异常: (failed) ne ...

  2. IIS6.0 IIS7.5应用程序池自动停止的解决方法

    前边提到由win2003升级到win2008 server r2 64位系统,然后用了几个小时配置IIS7.5+PHP+MYSQL等的环境,先是遇到IIS7.5下PHP访问慢的问题,解决之后又出了新的 ...

  3. python安装supervisor

    1.下载supervisor-3.3.1.tar.gz    http://pan.baidu.com/s/1dEPhGEd 2.下载meld3-1.0.2.tar.gz   http://pan.b ...

  4. linux操作系统基础

    计算机概述 1.计算机接收用户输入指令数据,经过cpu数据与逻辑单元运算处理后,产生或储存成有用的信息--->I/O设备+cpu+处理信息=计算机. 2.计算机五大单元:I/O单元 内存单元 c ...

  5. 那些年因为粗心导致的外链css无效

    css文件三种引用的三种方式: 1.外链: <link rel= "stylesheet" href=""> 注:如果使用外链式绝对不可以忘记 re ...

  6. vs 中统计代码行数

    ------解决方案--------------------b*[^:b#/]+.*$^b*[^:b#/]+.*$ ctrl + shift + F 查找选项勾选 正则表达式

  7. WebApi系列~基于RESTful标准的Web Api

    微软的web api是在vs2012上的mvc4项目绑定发行的,它提出的web api是完全基于RESTful标准的,完全不同于之前的(同是SOAP协议的)wcf和webService,它是简单,代码 ...

  8. 掌握Redmine

    一个带有建议.技巧和最佳实践的全面指导和易懂易学的结构. 掌握Redmine 版权©2013 Packt出版 前言(略) 1.熟悉Redmin 我们尝试去做一个新的网站应用程序的时候,回去询问一些了解 ...

  9. 1310. ACM Diagnostics

    http://acm.timus.ru/problem.aspx?space=1&num=1310 题目中说的 “the lexicographically increasing list” ...

  10. 使用 WinHttp 实现文件下载

    #include <Windows.h> #include <Winhttp.h> #pragma comment(lib,"Winhttp.lib") t ...