思路:指针p用于串联怎个链表,比较两个指针的大小,连接较小的一个。如果一个链表到达链尾,连接另外一个链表余下来的所以节点。

     public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode head;
ListNode p = null;
//判断初始链表是否为空
if (l1 == null && l2 == null) {
return null;
}
if(l1==null){
return l2;
}
if(l2==null){
return l1;
}
// 将最小的数作为head
if (l1.val <= l2.val) {
head = l1;
l1=l1.next;
} else {
head=l2;
l2=l2.next;
}
p=head;
// 比较两个链表大小,连接较小的一个
while (l1!= null && l2 != null) {
if(l1.val<=l2.val){
p.next=l1;
p=l1;
l1=l1.next;
}else{
p.next=l2;
p=l2;
l2=l2.next;
}
}
if(l1==null){ //如果l1到结尾,连接l2剩余部分
while(l2!=null){
p.next=l2;
p=l2;
l2=l2.next;
}
}else{ //否则连接l1剩余部分
while(l1!=null){
p.next=l1;
p=l1;
l1=l1.next;
}
}
return head;
}

【LeetCode-easy】Merge Two Sorted Lists的更多相关文章

  1. 【LeetCode练习题】Merge k Sorted Lists

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

  2. 【LeetCode OJ】Merge Two Sorted Lists

    题目:Merge two sorted linked lists and return it as a new list. The new list should be made by splicin ...

  3. 【LeetCode】【数组归并】Merge k Sorted Lists

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

  4. C# 写 LeetCode easy #21 Merge Two Sorted Lists

    21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list s ...

  5. 【Leetcode】【Easy】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 ...

  6. Leetcode练习题21. Merge Two Sorted Lists

    题目描述(easy) Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new ...

  7. 【leetcode】Merge k Sorted Lists

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

  8. [Leetcode][Python]21: Merge Two Sorted Lists

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 21: Merge Two Sorted Listshttps://oj.le ...

  9. [Leetcode][Python]23: Merge k Sorted Lists

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 23: Merge k Sorted Listshttps://oj.leet ...

  10. LeetCode LinkList 23. Merge k Sorted Lists

    这两天一直也没有顾上记录一下自己做过的题目,回头看看,感觉忘的好快,今天做了一个hard,刚开始觉得挺难得,想了两种方法,一种是每次都从k个list中选取最小的一个,为空的直接跳过,再就是每次合并其中 ...

随机推荐

  1. EasyMvc入门教程-基本控件说明(3)时间线

    我们有时候经常看到如下的页面: 或者快递物流信息图标,那么利用EasyMvc如何实现呢?很简单,看下面的例子: @{ var data=new List<TimeLineItem>() { ...

  2. 【共享单车】—— React后台管理系统开发手记:Router 4.0路由实战演练

    前言:以下内容基于React全家桶+AntD实战课程的学习实践过程记录.最终成果github地址:https://github.com/66Web/react-antd-manager,欢迎star. ...

  3. 2017.2.13 开涛shiro教程-第十二章-与Spring集成(二)shiro权限注解

    原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 第十二章-与Spring集成(二)shiro权限注解 shiro注 ...

  4. 2016.6.21 maven:Failure to transfer ... from ....

    问题描述: 才刚新建的工程,什么都没做,就显示pom.xml有问题,在第一行的标签上 有如下错误: 点击详情: Failure to transfer org.apache.maven:maven-p ...

  5. 控制面板的cpl程序列表

    控制面板的cpl程序列表 学习了:https://zhidao.baidu.com/question/2141898537654796628.html 最近用来sysdm.cpl: 辅助功能选项:ac ...

  6. git-for-windows 安装无图标的问题

    git-for-windows.ico 安装无图标的问题 一. 问题表现: 桌面图标与右建菜单图标,所是未知文件的图标, 二. 问题解决: 在shard/git/ copy 一个ico 文件(如git ...

  7. 习科小D整理找网站后台办法[科普]

    习科科普贴,如何找到网站的后台 作者:小Dの马甲来自:习科论坛 - Silic.Org - BlackBap.Org 1, 穷举猜解    现如今可以暴力猜解网站后台登陆地址的软件有很多,从最早的啊D ...

  8. 全国车辆违章查询API文档及demo

    简介 聚合数据全国车辆违章API,目前已经支持300个左右的城市违章查询,已连接上万个APP.方便有车一族随时了解自己是否有过交通违章,避免因遗忘或逾期处理违章罚单而造成的不必要损失. API参考文档 ...

  9. java查看工具jinfo-windows

    Generates configuration information. This command is experimental and unsupported. Synopsis jinfo [  ...

  10. .net mvc项目 ajax

    经常在后台用一般处理程序(.ashx)来处理前台的ajax请求 using System; using System.Collections.Generic; using System.IO; usi ...