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.

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if(l1==null)
return l2;
if(l2==null)
return l1;
ListNode headNode=new ListNode(0); //注意这里要先初始化
ListNode head=headNode;
head.next=null;
ListNode r=head,p=l1,q=l2;
while(p!=null&&q!=null){
if(p.val<=q.val){
r.next=p; //想想为什么要r.next. r不行是为什么
r=r.next;
p=p.next;
}
else{
r.next=q;
r=r.next;
q=q.next;
}
}
if(p!=null)
r.next=p;
else
r.next=q;
return head.next;
}
}

  

21.Merge Two Sorted Lists(链表)的更多相关文章

  1. Leetcode 21 Merge Two Sorted Lists 链表

    合并两个已排序的链表,考到烂得不能再烂的经典题,但是很多人写这段代码会有这样或那样的问题 这里我给出了我的C++算法实现 /** * Definition for singly-linked list ...

  2. 21. Merge Two Sorted Lists(合并2个有序链表)

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

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

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

  4. 21.Merge Two Sorted Lists 、23. Merge k Sorted Lists

    21.Merge Two Sorted Lists 初始化一个指针作为开头,然后返回这个指针的next class Solution { public: ListNode* mergeTwoLists ...

  5. 21. Merge Two Sorted Lists【easy】

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

  6. leetCode练题——21. Merge Two Sorted Lists(照搬大神做法)

    1.题目 21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new l ...

  7. 刷题21. Merge Two Sorted Lists

    一.题目说明 这个题目是21. Merge Two Sorted Lists,归并2个已排序的列表.难度是Easy! 二.我的解答 既然是简单的题目,应该一次搞定.确实1次就搞定了,但是性能太差: R ...

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

  10. 【LeetCode】21. Merge Two Sorted Lists 合并两个有序链表

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,有序链表,递归,迭代,题解,leetcode, 力 ...

随机推荐

  1. hibernate 双向一对多关系(Annotation mappedBy注解理解)

    1.@mappedBy 属性简单理解为设定为主表(OneToMany方)(这只是我个人理解,上面文章中也有提到过) 所以另一端(ManyToOne)则需要设置外键@JoinColumn(name=&q ...

  2. 搜索关注点--2014年的google关注点

    补充: 网站知名网站收录 网站被世界三大知名网站DMOZ,Yahoo和Looksmart收录 众所周知,Google的Pagerank系统对那些门户网络目录如 DMOZ,Yahoo和Looksmart ...

  3. [java]序列化框架性能对比(kryo、hessian、java、protostuff)

    序列化框架性能对比(kryo.hessian.java.protostuff) 简介:   优点 缺点 Kryo 速度快,序列化后体积小 跨语言支持较复杂 Hessian 默认支持跨语言 较慢 Pro ...

  4. openstack(liberty):部署实验平台(三,简单版本软件安装 之cinder,swift)

    今天这里追加存储相关的部署,主要是Block和Object,为了看到效果,简单的部署在单节点上,即Block一个节点,Object对应一个节点. 读者可能会觉得我这个图和之前的两个post有点点不同, ...

  5. CSS3 Media Queries(响应式布局可以让你定制不同的分辨率和设备)

    点评:Media Queries这功能是非常强大的,他可以让你定制不同的分辨率和设备,并在不改变内容的情况下,让你制作的web页面在不同的分辨率和设备下都能显示正常,并且不会因此而丢失样式   Med ...

  6. mysql中int转varchar

    这里要注意,cast(XX as varcahr(10))在mysql中不好使,要cast(XX as char(10))这样才好使

  7. IntelliJ IDEA优化总结

    1.修改JVM参数 (IntelliJ IDEA 10.0.1包含以上版本不需要设置)修改idea.exe.vmoptions配置文件调整以下内容:-Xms256m-Xmx384m-XX:MaxPer ...

  8. Python 字典和列表的对比应用

    Q:将下列格式的txt文件,打印出该选手的3个最快跑步时间 james2.txt =>“James Lee,2002-3-14,2-34,3:21,2.34,2.45,3.01,2:01,2:0 ...

  9. Linux下的NTP

    一.电脑时间的误差众 所周知,电脑主机的时间是根据电脑晶振以固定频率振荡,从而产生的.由于晶振的不同,会导致电脑时间与UTC时间 (全球标准时间:全球标准时间指的是由世界时间标准设定的时间.原先也被称 ...

  10. Node.js 相关资料网站汇总

    地址:https://cnodejs.org/ nodejs中文网:http://nodejs.cn/ nodejs中文网:http://www.nodejs.net/ 相关API地址:http:// ...