题目:

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.

题解:

这道题是链表操作题,题解方法很直观。

首先,进行边界条件判断,如果任一一个表是空表,就返回另外一个表。

然后,对于新表选取第一个node,选择两个表表头最小的那个作为新表表头,指针后挪。

然后同时遍历两个表,进行拼接。

因为表已经是sorted了,最后把没有遍历完的表接在新表后面。

由于新表也会指针挪动,这里同时需要fakehead帮助记录原始表头。

代码如下:

 1     public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
 2         if(l1==null)
 3             return l2;
 4         if(l2==null)
 5             return l1;
 6             
 7         ListNode l3;
 8         if(l1.val<l2.val){
 9             l3 = l1;
             l1 = l1.next;
         }else{
             l3 = l2;
             l2 = l2.next;
         }
         
         ListNode fakehead = new ListNode(-1);
         fakehead.next = l3;
         while(l1!=null&&l2!=null){
             if(l1.val<l2.val){
                 l3.next = l1;
                 l3 = l3.next;
                 l1 = l1.next;
             }else{
                 l3.next = l2;
                 l3 = l3.next;
                 l2 = l2.next;
             }
         }
         
         if(l1!=null)
             l3.next = l1;
         if(l2!=null)
             l3.next = l2;
         return fakehead.next;
     }

更简便的方法是,不需要提前选新表表头。

对于新表声明两个表头,一个是fakehead,一个是会挪动的指针,用于拼接。同时,边界条件在后面的补拼中页解决了,所以开头没必要做边界判断,这样代码简化为:

 1     public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
 2         ListNode fakehead = new ListNode(-1);
 3         ListNode l3 = fakehead;
 4         while(l1!=null&&l2!=null){
 5             if(l1.val<l2.val){
 6                 l3.next = l1;
 7                 l3 = l3.next;
 8                 l1 = l1.next;
 9             }else{
                 l3.next = l2;
                 l3 = l3.next;
                 l2 = l2.next;
             }
         }
         
         if(l1!=null)
             l3.next = l1;
         if(l2!=null)
             l3.next = l2;
         return fakehead.next;
     }

Merge Two Sorted Lists leetcode java的更多相关文章

  1. Merge k Sorted Lists Leetcode Java

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

  2. Merge Two Sorted Lists - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Merge Two Sorted Lists - LeetCode 注意点 两个链表长度可能不一致 解法 解法一:先比较两个链表长度一致的部分,多余的部分 ...

  3. 23. Merge k Sorted Lists - LeetCode

    Question 23. Merge k Sorted Lists Solution 题目大意:合并链表数组(每个链表中的元素是有序的),要求合并后的链表也是有序的 思路:遍历链表数组,每次取最小节点 ...

  4. Merge Two Sorted Lists—LeetCode

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  5. Merge k Sorted Lists [LeetCode]

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

  6. 蜗牛慢慢爬 LeetCode 23. Merge k Sorted Lists [Difficulty: Hard]

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

  7. Java for LeetCode 023 Merge k Sorted Lists

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

  8. [LeetCode] 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. shell 变量中间有空格 如何传入整个变量

    parmfile='abc  123' RunProgram programname "${parmfile}"  -->传入abc 123 RunProgram progr ...

  2. shell run 屏蔽一切log

    &命令: xxx >/dev/null 2>&1 &                           屏蔽一切logxxx >/tmp/xxx.log 2 ...

  3. 回顾2014 Java发生的5件大事

    回顾2014 Java发生的5件大事 1.2月1日:RedMonk分析师确认并宣布Java是最受欢迎和多样化的语言! 2014年,Java生态圈伴随着引擎的轰鸣起步,随着FOSDEM年会的Free J ...

  4. Django扩展

    一.文件上传 当Django在处理文件上传的时候,文件数据被保存在request.FILES FILES中的每个键为<input type="file" name=" ...

  5. Winform 串口通讯之读卡器

    老板给我的第一个硬件就是一个读卡器, 说让我做一下试试,于是从网上查了查就写了出来,相当的简单. 但是后来还有一个地磅的串口通讯,我整整搞了一天. 在窗体类的构造函数中写入 Form.CheckFor ...

  6. android studio 汉化 个性化 美化 快速操作项目 目录

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 汉化包 百度云盘 下载地址:https://pan.baidu.com/s/1pLjwy ...

  7. luogu4770 [NOI2018]你的名字 后缀自动机 + 线段树合并

    其实很水的一道题吧.... 题意是:每次给定一个串\(T\)以及\(l, r\),询问有多少个字符串\(s\)满足,\(s\)是\(T\)的子串,但不是\(S[l .. r]\)的子串 统计\(T\) ...

  8. Codeforces Round #228 (Div. 1) C. Fox and Card Game 博弈

    C. Fox and Card Game 题目连接: http://codeforces.com/contest/388/problem/C Description Fox Ciel is playi ...

  9. ZOJ 3632 K - Watermelon Full of Water 优先队列优化DP

    K - Watermelon Full of Water Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%lld &am ...

  10. Polly简介 — 1. 故障处理策略

    Polly 是 .Net 下的一套瞬时故障处理及恢复的函式库,可让开发者以fluent及线程安全的方式来应用诸如Retry.Circuit Breaker.Timeout.Bulkhead Isola ...