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. php empty,isset,is_null比较(差异与异同)

    php empty,isset,is_null比较(差异与异同)  http://www.cnblogs.com/chengmo/archive/2010/10/18/1854258.html

  2. std::map用法

    STL是标准C++系统的一组模板类,使用STL模板类最大的好处就是在各种C++编译器上都通用.    在STL模板类中,用于线性数据存储管理的类主要有vector, list, map 等等.本文主要 ...

  3. ElasticSearch配置说明

    配置文件位于%ES_HOME%/config/elasticsearch.yml文件中. cluster.name: elasticsearch 配置集群名称,默认elasticsearch node ...

  4. 【思路】-OctService服务类

    OctService服务类 从以下几个方面来说吧,这次说的会有点长啊 设计: 思路: 作用: 目的: 问题: 为什么要设计这个? 它解决了什么问题? 是什么? 为什么? 怎么样? OctService ...

  5. mydumper 快速高效备份mysql,按照表生成备份文件,快速恢复

    Mydumper是一个针对MySQL和Drizzle的高性能多线程备份和恢复工具.开发人员主要来自MySQL,Facebook,SkySQL公司.目前已经在一些线上使用了Mydumper. Mydum ...

  6. qt越来越好了

    qml中所有的商业控件都开源了,详见: import QtQuick.Extras 1.4 以前自己实现的时候实现了半天.

  7. EasyUI表单内容整理

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  8. Win7搭建nginx+php+mysql开发环境以及websocket聊天实例测试

    Win7搭建nginx+php+mysql开发环境以及websocket聊天实例测试一.下载相关安装包 1.下载nginx最新版本(nginx1.3.13版之后才支持websocket协议) 下载地址 ...

  9. javascript如何用户的判断操作系统

    <script> alert(window.navigator.userAgent); if(window.navigator.userAgent.indexOf("Window ...

  10. ROS语音交互——科大讯飞语音合成TTS(二)

    之前我用过科大讯飞的语音包,为了记录一下我重新使用一下 首先注册科大讯飞账号及应用,以后每个下载的在线使用SDK都是以此账户ID登录讯飞语音服务器. 下载科大讯飞在线合成包. $ unzip Linu ...