思路:指针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. hibernate多对一单向配置

    查看:http://blog.csdn.net/u010702229/article/details/13170263

  2. Ethernet帧格式

    一.Ethernet帧格式的发展 1980 DEC,Intel,Xerox制订了Ethernet I的标准 1982 DEC,Intel,Xerox又制订了Ehternet II的标准 1982 IE ...

  3. 微信小程序-wxs

    你想在页面上使用JavaScript代码吗? 对不起,小程序不支持! 最近,一个项目就有这样的需求,我也就用上了wxs 使用方法很简单: 项目中用的是取小数点2位以及5位 具体请看官方API:WXS

  4. ie 浏览器无法保存cookie,且与域名包括了下划线(_)有关系的问题

    <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...

  5. objc_msgSend 报错

    NSMutableArray * mutableArray = [NSMutableArray arrayWithArray:array]; objc_msgSend(mutableArray,@se ...

  6. 上门洗车APP --- Androidclient开发 之 网络框架封装介绍(二)

    上门洗车APP --- Androidclient开发 之 网络框架封装介绍(二) 前几篇博文中给大家介绍了一下APP中的基本业务及开发本项目使用的网络架构: 上门洗车APP --- Androidc ...

  7. webStorm 多列编辑

    webStorm可以像Sublime一样使用列编辑,只是区别在于webStorm只可以编辑连续列表. 按住alt键鼠标选择一列,然后输入文字就会编辑多行,这个功能很赞,比较实用(按住ALT键选中之后, ...

  8. Chrome自带恐龙小游戏的源码研究(二)

    在上一篇<Chrome自带恐龙小游戏的源码研究(一)>中实现了地面的绘制和运动,这一篇主要研究云朵的绘制. 云朵的绘制通过Cloud构造函数完成.Cloud实现代码如下: Cloud.co ...

  9. servletResponse 控制浏览器缓存

    //当访问一些资源文件时,我们希望,访问一次后,资源文件能够在缓存在浏览器中,当我们再次访问该资源时 //直接从缓存中去取,这样可以减少服务器的压力 package response; import ...

  10. A20地址线问题

    [0]README text description from Zhaojiong's perfect analysis of Linux kernel . [1]A20地址线问题(干货来了) 198 ...