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. html之input系列标签

    input属性太多,我这里仅列出几个我喜欢的吧. disabled:首次加载时禁用此元素 checked:首次加载时选中此元素 form:输入字段所属的一个或多个表单 hieght:定义input字段 ...

  2. redis配置文件解析

    Redis是一个简单高效的内存KV数据库,基本上下载源码make install,编译完成,然后进入src目录运行redis-server即可运行.就是因为这么简单往往有朋友直接运行,将没有密码的re ...

  3. 使用 jQuery UI Widget Factory 编写有状态的插件(Stateful Plugins)

    使用 jQuery UI Widget Factory 编写有状态的插件(Stateful Plugins) Note 这一章节的内容是基于 Scott Gonzalez 一篇博客 Building ...

  4. eclipse下gradle配置

    eclipse下gradle配置 这可真麻烦啊,一个项目把java的东西学习完了!这不,刚弄完maven又来了个gradle. http://gradle.org/gradle-download 1. ...

  5. 【java】 java 集合类UML图

  6. js之引用类型

    一.摘要: <javascript高级程序设计第三版>一书中单独有一章对js的引用类型(Object.Array.RegExp.Function:基本包装类型:Boolean.Number ...

  7. Linux下编译UnixODBC

    编译环境 操作系统: Red Hat Enterprise Linux Server release 5.4 64-bit 编译工具: gcc (GCC) 4.1.2 20080704 (Red Ha ...

  8. Linux下做软RAID

    1.查看有多少块硬盘可用#fdisk -l嗯,一般而言,留下系统盘不动,其它的盘如果大小相同的话,统统合起来做一个raid. 2.决定做什么类型的raid.raid-0.raid-1.raid-5?如 ...

  9. select,epool,pool解释

    内容主要来自搜狗实验室技术交流文档, 编写链接数巨大的高负载服务器程序时,经典的多线程模式和select模式都不再适合了.应该采用epool/kqueue/dev_pool来捕获IO事件. ----- ...

  10. nginx 负载均衡-- 常用nginx配置

    中文官方网站http://wiki.nginx.org/Chshttp://www.howtocn.org/ --------------------------------------------- ...