021 Merge Two Sorted Lists 合并两个有序链表
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.
Example:
Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4
详见:https://leetcode.com/problems/merge-two-sorted-lists/description/
Java实现:
非递归:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if(l1==null){
return l2;
}
if(l2==null){
return l1;
}
if(l1==null&&l2==null){
return null;
}
ListNode head=new ListNode(-1);
ListNode cur=head;
while(l1!=null&&l2!=null){
if(l1.val<l2.val){
cur.next=l1;
l1=l1.next;
}else{
cur.next=l2;
l2=l2.next;
}
cur=cur.next;
}
cur.next=l1==null?l2:l1;
return head.next;
}
}
递归:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if(l1==null&&l2==null){
return null;
}
if(l1==null){
return l2;
}
if(l2==null){
return l1;
}
ListNode head=null;
if(l1.val<l2.val){
head=l1;
head.next=mergeTwoLists(l1.next,l2);
}else{
head=l2;
head.next=mergeTwoLists(l1,l2.next);
}
return head;
}
}
021 Merge Two Sorted Lists 合并两个有序链表的更多相关文章
- leetcode 21 Merge Two Sorted Lists 合并两个有序链表
描述: 合并两个有序链表. 解决: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { if (!l1) return l2; if (!l2) ...
- LeetCode 21. Merge Two Sorted Lists合并两个有序链表 (C++)
题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...
- 【LeetCode】21. Merge Two Sorted Lists 合并两个有序链表
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,有序链表,递归,迭代,题解,leetcode, 力 ...
- 【LeetCode】Merge Two Sorted Lists(合并两个有序链表)
这道题是LeetCode里的第21道题. 题目描述: 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1-&g ...
- [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 ...
- [LeetCode] Merge k Sorted Lists 合并k个有序链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 这 ...
- [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 ...
- 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 ...
- 023 Merge k Sorted Lists 合并K个有序链表
合并K个有序链表,并且作为一个有序链表的形式返回.分析并描述它的复杂度. 详见:https://leetcode.com/problems/merge-k-sorted-lists/descripti ...
随机推荐
- druid数据源的加密解密工具
数据库得加密 先来一个网上大多数的教程吧,一个比较好的教程,如下. jar包版本:druid-1.0.13.jar 1. 加密,用以下命令将用户名和密码加密 cmd命令行执行 java -cp D:/ ...
- 发布倒计时!JDK11为我们带来哪些新特性?
今年7月底,JDK11已经进入了Rampdown Phase Two阶段,这标志着该版本所有特性已经被冻结,不会有新的JEP会加入版本中. 这一阶段将会修复P1–P2级BUG,之后,JDK11预定于今 ...
- BluetoothFindRadioClose 函数
BOOL BluetoothFindRadioClose( HBLUETOOTH_RADIO_FIND hFind );关闭与查找蓝牙无线电相关的枚举句柄.参数: hFind Enumeration ...
- Matlab常用函数(1)
1.max() C = max(A) A为向量,返回最大值.若为矩阵,以类向量为基准,返回每列的最大值的行向量.若为多维矩阵.切片返回每一个2维矩阵的行向 量. C = max(A,B) ...
- 9.利用msfvenom生成木马
这篇文章来介绍一下msf中一个生成木马的msfvenom模块. msfvenom命令行选项如下: 英文原版: 中文版: Options: -p, --payload <payload> 指 ...
- asp.net刷新本页面的六种方法总结
第一: private void Button1_Click( object sender, System.EventArgs e ) { Response.Redirect( Reque ...
- 1、jquery_属性和选择器
1.ID选择器 2.attr和val和removeattr 3.attr和removeattr和val <html> <head> <meta charset=" ...
- C++哪些函数不能是虚函数
1. inline是编译时展开,必须有实体:(不考虑不展开的假inline) 2. static属于class自己的,也必须有实体: 3. 构造函数.复制构造函数.virtual函数基于vta ...
- 深度学习之Keras
Keras简介 Keras是一个高层神经网络API,Keras完全由Python编写而成,使用Tensorflow.Theano及CNTK作为后端. 通过Python脚本查看Keras使用的后端 输出 ...
- 洛谷P2052 道路修建
P2052 道路修建 题目描述 在 W 星球上有 n 个国家.为了各自国家的经济发展,他们决定在各个国家 之间建设双向道路使得国家之间连通.但是每个国家的国王都很吝啬,他们只愿 意修建恰好 n – 1 ...