链表经典题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.
/**
* 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;
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 ;
}
};
链表经典题Merge Two Sorted Lists的更多相关文章
- [LC]21题 Merge Two Sorted Lists (合并两个有序链表)(链表)
①英文题目 Merge two sorted linked lists and return it as a new list. The new list should be made by spli ...
- (链表) 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第22题--Merge k Sorted Lists
problem:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its compl ...
- 两个升序链表的合并 Merge Two Sorted Lists 【 leetcode】
class Solution {public: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { ListNode *p; ListN ...
- 【LeetCode】23. Merge k Sorted Lists 合并K个升序链表
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,链表,单链表,题解,leetcode, 力扣,Py ...
- [LeetCode] Merge k Sorted Lists 合并k个有序链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 这 ...
- LeetCode专题-Python实现之第21题:Merge Two Sorted Lists
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- [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 ...
随机推荐
- ionic view 视图
ionic view 方法 $ionicView.loaded 视图已经被加载了.这事件只发生一次当视图被创建并添加到Dom中.当跳出页面并且被缓存了的话,再次访问这个页面时这个时间将不会被激活.L ...
- PHP+Ajax点击加载更多内容 -这个效果好,速度快,只能点击更多加载,不能滚动自动加载
这个效果好,速度快,只能点击更多加载,不能滚动自动加载 一.HTML部分 <div id="more"> <div class="single_item ...
- 记一次msf入侵win10,并拍照
好久没有玩kali了,刚才看到一位大佬msf渗透win10的思路,我感觉不错,我就来复现一下 kali :192.168.45.136 win10 : 192.168.45.137 1 首先,我们查 ...
- HashMap基础知识
HashMap 简介 HashMap 主要用来存放键值对,它基于哈希表的Map接口实现,是常用的Java集合之一. JDK1.8 之前 HashMap 由 数组+链表 组成的,数组是 HashMap ...
- IO流16 --- 对象流操作字符串 --- 技术搬运工(尚硅谷)
序列化 @Test public void test12() throws IOException { ObjectOutputStream oos = new ObjectOutputStream( ...
- HDFS读数据的过程
- Spring事务传播行为详解
前言 Spring在TransactionDefinition接口中规定了7种类型的事务传播行为.事务传播行为是Spring框架独有的事务增强特性,他不属于的事务实际提供方数据库行为.这是Spring ...
- C# params 用法简介
params 是C#的关键字, params主要是在声明方法时参数类型或者个数不确定时使用,关于params 参数数组,需掌握以下几点: 一.参数数组必须是一维数组 二.不允许将params修饰符与r ...
- FreeMarker 获取页面request、session
使用Request里的Attribute值最简单的方法就是直接${AttributeName}或者安全一点:${AttributeName!"default Value"} 1.取 ...
- storm 为什么要存在不透明分区事务
不透明分区事务不区分发新消息还是旧消息,全部用emitPartitionBatch搞定,虽然 emitPartitionBatch返回的X应该是下一批次供自己使用(emitPartitionBatch ...