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. nullable,nonnull, null_resettable以及_Null_unspecified的区别和使用

    1.关键字:可以用于属性 方法和返回值参数中 关键字作用:提示作用  告诉开发者属性信息 关键字的目的:迎合swift 强语言,swift必须要指定一个对象是否为空 关键字好处:提高代码规划,减少沟通 ...

  2. WinForm 多窗体

    多窗体:一.首先要想到的问题是:1.哪个是主窗体问题:主窗体隐藏了,关闭其它窗体后,没有将主窗体显示/关闭,那么程序就关不上了 方法:用构造函数传值,将窗体传到另一个窗体中去 Form1: using ...

  3. 删除ORACLE的步骤

    1.关闭oracle所有的服务.可以在windows的服务管理器中关闭: 2.打开注册表:regedit 打开路径: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlS ...

  4. winform 实现pdf浏览

    1.开发工具:VS2013 2.开发环境:win 10 64位 3.添加控件: 打开VS,点击工具箱--常规--右键--“选择项”---“Com组件”,选择Adobe PDF Reader控件(在这之 ...

  5. javascript实现列表的点击展开折叠

    <script> window.onload = function() { //要折叠的区域 var catalog = document.getElementById("div ...

  6. RHEL 6.5 PXE 安装调试 VMWARE ESXi 5.1 安装

    测试环境:win10/vmware workstation 12 pro 操作系统:RHEL 6.5(PXE服务器) [2016.11.30] [遗留问题] 1.ks.cfg通过本地文件方式加载不成功 ...

  7. bzoj3504: [Cqoi2014]危桥

    题意:给出一个图,有的边可以无限走,有的只能走两次(从一头到另一头为一次),给定两个起点以及对应的终点以及对应要走几个来回,求判断是否能完成. 先来一个NAIVE的建图:直接限制边建为容量1,无限制为 ...

  8. WCF传输图片解决方案

    图片无法序列化后传输,但我们可以将图片转二进制字符串传输.然后在服务端将二进制字符串转图片. 将图片转字符串的例子: private byte[] BmpToJpegBuff(Image img) { ...

  9. Android 设置ListView当前显示的item

    项目中可能会有这种需求:动态设置ListView显示的item 这种需求可能会出现在不同的情况下,有的是打开页面就要显示在特定的位置,也有的是浏览列表时实时更新数据并且改变了集合中数据,或者是某种条件 ...

  10. React Native 使用问题记录

    1.<View></View>之间有空格会报错 Trying to add unknown view tag 2.一些js语法糖注意点http://facebook.githu ...