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 list should be made by splicing together the nodes of the first two lists.
解法一:
/**
* 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 || l2 == NULL) {
return l1 ? l1 : l2;
} ListNode * dummy = new ListNode(INT_MIN);
ListNode * temp = dummy; while (l1 && l2) {
if (l1->val > l2->val) {
dummy->next = l2;
l2 = l2->next;
}
else {
dummy->next = l1;
l1 = l1->next;
} dummy = dummy->next;
} if (l1 || l2) {
dummy->next = l1 ? l1 : l2;
} return temp->next;
}
};
由于最后是弄到list1中,但是我们不知道list1还是list2的第一个元素关系,最后结果的list1中的头结点可能会改变,所以需要引入dummy节点。
解法二:
public ListNode mergeTwoLists(ListNode l1, ListNode l2){
if(l1 == null) return l2;
if(l2 == null) return l1;
if(l1.val < l2.val){
l1.next = mergeTwoLists(l1.next, l2);
return l1;
} else{
l2.next = mergeTwoLists(l1, l2.next);
return l2;
}
}
参考了@yangliguang 的代码
解法三:
public class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if (l1 == null) return l2;
if (l2 == null) return l1;
ListNode handler;
if(l1.val < l2.val) {
handler = l1;
handler.next = mergeTwoLists(l1.next, l2);
} else {
handler = l2;
handler.next = mergeTwoLists(l1, l2.next);
}
return handler;
}
}
参考了@RunRunCode 的代码
解法二和解法三都是递归,还没有完全弄明白……
21. Merge Two Sorted Lists【easy】的更多相关文章
- Leetcode 21. Merge Two Sorted Lists(easy)
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...
- # 蜗牛慢慢爬 LeetCode 21. Merge Two Sorted Lists [Difficulty: Easy]
题目 Merge two sorted linked lists and return it as a new list. The new list should be made by splicin ...
- 两个升序链表的合并 Merge Two Sorted Lists 【 leetcode】
class Solution {public: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { ListNode *p; ListN ...
- LeetCode:21. Merge Two Sorted Lists(Easy)
1. 原题链接 https://leetcode.com/problems/merge-two-sorted-lists/description/ 2. 题目要求 给出两个已经从小到大排序的链表ls1 ...
- 88. Merge Sorted Array【easy】
88. Merge Sorted Array[easy] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 ...
- 刷题21. Merge Two Sorted Lists
一.题目说明 这个题目是21. Merge Two Sorted Lists,归并2个已排序的列表.难度是Easy! 二.我的解答 既然是简单的题目,应该一次搞定.确实1次就搞定了,但是性能太差: R ...
- [Leetcode][Python]21: Merge Two Sorted Lists
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 21: Merge Two Sorted Listshttps://oj.le ...
- 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 ...
- 160. Intersection of Two Linked Lists【easy】
160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersecti ...
随机推荐
- 【动态规划】bzoj1633 [Usaco2007 Feb]The Cow Lexicon 牛的词典
f[i]=min{f[i+1]+1,f[i+len[j]+cant]+cant}(for i=L-1 downto 0)(1<=j<=w) #include<cstdio> # ...
- 千克与磅之间的转换 Exercise05_05
/** * @author 冰樱梦 * 时间:2018年下半年 * 题目:千克与磅之间的转换 * */ public class Exercise05_05 { public static void ...
- 微服务之SpringCloud实战(一):SpringCloud简介
什么是微服务架构 微服务架构就是系统架构设计的一种风格,它主旨将一个独立的系统,拆分成各个微服务,各个微服务独立运行,他们之间通过Http的Restful API进行通信,拆分出来的微服务是根据原系统 ...
- Linux下使用split按行数进行切割
说明:一般来说split按行数切割多数用在日志文件上. 实例: 将一个大文件分成若干个小文件方法: 例如将一个BLM.txt文件分成前缀为BLM_ 的1000个小文件,后缀为系数形式,且后缀为4位数字 ...
- es6类声明,class总结
1.class的基本写法 class a{ // 传入参数或者写入固定参数 constructor(a,b){ this.a=a this.b=b } // 可直接调用的计算后的参数 get c(){ ...
- ClipboardJS复制粘贴插件的使用
1.简单的纯JS复制粘贴(兼容性差,只能用textarea标签) var btn=document.getElementsByClassName("btn")[0]; //复制按钮 ...
- Array.apply 方法的使用
Array.apply(null, {length: 5}) length为特殊字段,意思是生成一个长度为5的数组,由于没赋值,所以都是undefined; 如果要赋值,可以这样 console.lo ...
- Yii2系列教程三:Database And Gii
上一篇文章我们理了一下Yii2的MVC,Forms和Layouts,这篇文章就直接按照约定来说说Yii2与数据库相关的一些事情,如果你觉得不够的话,不急,更具体的用法我会在后续的教程给出,并且这里也会 ...
- sqoop操作与使用
sqoop只要安装到集群中的一台节点就可以了 1.上传sqoop到节点中 2.安装和配置 在添加sqoop到环境变量到/etc/profile中 将数据库连接驱动拷贝到$SQOOP_HOME/lib里 ...
- mysql 5.7.13 安装配置方法(linux)-后期部分运维
mysql 5.7.13 安装配置方法图文教程(linux) 学习了:https://www.cnblogs.com/zhao1949/p/5947938.html /usr/local/mysql是 ...