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 should be made by splicing together the nodes of the first two lists
需要排序!!!
[2,4]
[1]
输出1 2 4
而不是2 4 1
递归版!!
class Solution {
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(l2.next,l1);
return l2;
}
}
}
21. Merge Two Sorted Lists(合并2个有序链表)的更多相关文章
- 【LeetCode】21. Merge Two Sorted Lists 合并两个有序链表
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,有序链表,递归,迭代,题解,leetcode, 力 ...
- leetcode 21 Merge Two Sorted Lists 合并两个有序链表
描述: 合并两个有序链表. 解决: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { if (!l1) return l2; if (!l2) ...
- [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 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] 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 ...
- 023 Merge k Sorted Lists 合并K个有序链表
合并K个有序链表,并且作为一个有序链表的形式返回.分析并描述它的复杂度. 详见:https://leetcode.com/problems/merge-k-sorted-lists/descripti ...
- 【LeetCode每天一题】 Merge k Sorted Lists(合并K个有序链表)
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. E ...
- 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 t ...
随机推荐
- Oracle-随机数获取
1.获取10-100的数据,保留两位小数 select trunc(dbms_random.value(10,100),2) from dual ; 2.获取0-1的小数 select dbms_ra ...
- PHP面向对象 实例化 构造函数 封装 继承 静态
PHP面向对象 实例化 构造函数 封装 继承 静态 面向对象: 一:定义类 class Dog { var $name; var $age; var $pinzhong; function Jiao( ...
- CSDN专栏收集
Android集 1.Himi李华明的<Android游戏开发专栏>http://blog.csdn.net/column/details/androidgame.html2.老罗的&l ...
- poj3734 Blocks[矩阵优化dp or 组合数学]
Blocks Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6578 Accepted: 3171 Descriptio ...
- 4.querystring属性
1.querystring.stringify(obj[, sep[, eq[, options]]]) 序列化, 第二个参数分隔符, 第三个参数是对象分隔符 querystring.stringif ...
- 170801、VM性能调优
最近因项目存在内存泄漏,故进行大规模的JVM性能调优 , 现把经验做一记录. 一.JVM内存模型及垃圾收集算法 1.根据Java虚拟机规范,JVM将内存划分为: New(年轻代) Tenured(年老 ...
- html 中 div 盒子并排展示
在项目中,遇到一个前端问题,觉得小问题就别去找前端工程师解决了,还是自己动动手吧. 相信不管小问题,大问题 都应该先自己尝试解决,google .度娘查查资料,这绝对是增加理解和记忆的好机会. ##问 ...
- 【elasticsearch 依赖 urllib3 请问 是否 urllib3和阿里es、oss的对接出现异常】
During handling of the above exception, another exception occurred: Traceback (most recent call last ...
- Python开发【数据结构】:排序练习
排序练习 问题一: 现在有一个列表,列表中的数范围都在0到100之间,列表长度大约为100万.设计算法在O(n)时间复杂度内将列表进行排序. import random data = [random. ...
- Python开发【Django】:Form组件
Form组件 Django的Form主要具有一下几大功能: 生成HTML标签 验证用户数据(显示错误信息) HTML Form提交保留上次提交数据 初始化页面显示内容 创建Form类时,主要涉及到 [ ...