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.

思想和merge sort一样,只是要考虑相等的情况的时候该怎么办。

/*** 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 result = new ListNode(0);ListNode returnResult = result;while (l1 != null && l2 != null) {while (l1 != null && l2 != null && l1.val < l2.val) {result.next = l1;l1 = l1.next;result = result.next;}while (l1 != null && l2 != null && l1.val > l2.val) {result.next = l2;l2 = l2.next;result = result.next;}while (l1 != null && l2 != null && l1.val == l2.val) {result.next = l1;l1 = l1.next;result = result.next;result.next = l2;l2 = l2.next;result = result.next;}}if (l1 == null) {result.next = l2;} else {result.next = l1;}return returnResult.next;}
}

写的还是有些冗余了。。。

简洁版:

public class Solution {public ListNode mergeTwoLists(ListNode l1, ListNode l2) {if (l1 == null) {return l2;}if (l2 == null) {return l1;}ListNode result = new ListNode(0);ListNode returnResult = result;while (l1 != null && l2 != null) {if (l1.val < l2.val) {result.next = l1;l1 = l1.next;} else {result.next = l2;l2 = l2.next;}result = result.next;}if (l1 == null) {result.next = l2;} else {result.next = l1;}return returnResult.next;}
}

没想到还可以用recursion做

public class Solution {public ListNode mergeTwoLists(ListNode l1, ListNode l2) {if (l1 == null) {return l2;}if (l2 == null) {return l1;}ListNode result = null;if (l1.val < l2.val) {result = l1;result.next = mergeTwoLists(l1.next, l2);} else {result = l2;result.next = mergeTwoLists(l1, l2.next);}return result;}
}

时隔两个月再来做这道题,发现自己还是进步了的~至少直接写出了简洁版。吼吼吼

也又写了一遍recursion

public class Solution {public ListNode mergeTwoLists(ListNode l1, ListNode l2) {if (l1 == null) {return l2;}if (l2 == null) {return l1;}if (l1.val > l2.val) {l2.next = mergeTwoLists(l1, l2.next);return l2;} else {l1.next = mergeTwoLists(l1.next, l2);return l1;}}
}

C++版本:

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode(int x) : val(x), next(NULL) {}* };*/
class Solution {
public:ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {if (l1 == NULL) {return l2;}if (l2 == NULL) {return l1;}if (l1 -> val > l2 -> val) {l2 -> next = mergeTwoLists(l1, l2 -> next);return l2;} else {l1 -> next = mergeTwoLists(l1 -> next, l2);return l1;}}
};

c++另一种写法怎么也写不过。。。不懂指针什么的,到时候懂了再来写吧。。。#未完待续#需回顾。。。

转载于:https://www.cnblogs.com/aprilyang/p/6620460.html

Merge Two Sorted Lists Leetcode相关推荐

  1. Merge Two Sorted Lists LeetCode

    ---恢复内容开始--- Merge two sorted linked lists and return it as a new list. The new list should be made ...

  2. Merge Two Sorted Lists leetcode java

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

  3. Merge k Sorted Lists leetcode java

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

  4. LeetCode刷题记录15——21. Merge Two Sorted Lists(easy)

    LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) 目录 LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) ...

  5. LeetCode 之 JavaScript 解答第23题 —— 合并K个有序链表(Merge K Sorted Lists)

    Time:2019/4/10 Title: Merge K Sorted Lists Difficulty: Difficulty Author: 小鹿 题目:Merge K Sorted Lists ...

  6. LeetCode算法入门- Merge Two Sorted Lists -day15

    LeetCode算法入门- Merge Two Sorted Lists -day15 题目描述: Merge two sorted linked lists and return it as a n ...

  7. [LeetCode] Merge k Sorted Lists 合并k个有序链表

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

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

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

  9. Leetcode 21:Merge Two Sorted Lists(golang实现合并两条已经排序的链表)

    21.Merge Two Sorted Lists 题目链接:题目链接 Merge two sorted linked lists and return it as a new list. The n ...

  10. 合并k个有序链表 python_[LeetCode] 23. Merge k Sorted Lists 合并k个有序链表

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

最新文章

  1. (原创)按照一定的格式生成一定数量的随机数的例子
  2. 康宁玻璃ct值计算公式_CT原理(一)
  3. 深划痕需要大面积补漆吗_剐蹭了需要立马补漆吗?这些小技巧能省不少!
  4. WPF在预览视图下可以看到图片,运行时却报错提示找不到资源
  5. 运维工程师如果将web服务http专变为https
  6. python视频提取音频_python脚本实现mp4中的音频提取并保存在原目录
  7. js学习小计6-慎用return false;
  8. 增强幸福感的五种方法
  9. 如何成为一名出色的演说者
  10. android安装管理,android-使用下载管理器下载后安装apk,并退出...
  11. fis 前端构建工具
  12. vue使用wangeditor自定义表情替换QQ表情
  13. ffmpeg学习笔记(2)-YUV420算法原理
  14. Springboot学习-MD5盐值密码加密 DigestUtils 和 BCryptPasswordEncoder
  15. 笔记本计算机没反应怎么办,笔记本电脑开机黑屏没反应怎么办?
  16. html5 按钮css样式修改,css样式制作的漂亮按钮
  17. html设置闹钟提醒,设置闹钟标签.html
  18. FCKEDITOR编辑器的使用
  19. 不知道PDF转PPT转换器哪个好用?分享三个简单好用的办公用具
  20. IDM下载器软件激活序列号错误如何解决?

热门文章

  1. linux nfs文件共享
  2. 如何进行多语言发布,做国际化开发
  3. centos7 vsftpd 虚拟用户 pam模块认证
  4. Xcode 模拟器复制解决方案
  5. 搭建Open××× Server路由模式、证书认证
  6. 算法与数据结构(二)三元组矩阵行列式的计算(用递归)
  7. .NetCore中的程序通过Docker在CentOS中部署
  8. 316 Remove Duplicate Letters 去除重复字母
  9. FragmentPagerAdapter加载fragment并使用setUserVisibleHint()处理预加载时遇到的坑,给textview赋值时出现的空指针异常...
  10. windows服务应用--做个简单的定时调用EXE功能(笔记)