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.

Hide Tags

Linked List

/**
* 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的更多相关文章

  1. [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 ...

  2. (链表) 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 ...

  3. leetcode第22题--Merge k Sorted Lists

    problem:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its compl ...

  4. 两个升序链表的合并 Merge Two Sorted Lists 【 leetcode】

    class Solution {public:   ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {   ListNode *p; ListN ...

  5. 【LeetCode】23. Merge k Sorted Lists 合并K个升序链表

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,链表,单链表,题解,leetcode, 力扣,Py ...

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

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

  7. LeetCode专题-Python实现之第21题:Merge Two Sorted Lists

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  8. [Leetcode] Merge k sorted lists 合并k个已排序的链表

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

  9. [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. innodb状态

    Innodb_buffer_pool_pages_data Innodb buffer pool缓存池中包含数据的页的数目,包括脏页.单位是page. Innodb_buffer_pool_pages ...

  2. 全栈之路-微信小程序-SKU开发(分析)

    SKU是整个小程序中最难完成的部分了,好好记录一下SKU,主要是想记录一下 从最开始拿到这个业务到最终完成这个功能期间的思考过程,至于代码什么的,记录也好,不记录也行,再看! 一.从思路说起 1.SK ...

  3. Spring_关于@Resource注入为null解决办法

    初学spring,我在dao层初始化c3p0的时候,使用@Resource注解新建对象是发现注入为null,告诉我 java.lang.NullPointerException. @Repositor ...

  4. Django项目:CMDB(服务器硬件资产自动采集系统)--10--06CMDB测试Linux系统采集硬件数据的命令05

    cd /py/AutoClient/bin python3 auto-client.py /usr/local/python3/bin/pip install requests python3 aut ...

  5. MySQL命令行本地登陆,远程登陆MySQL 的快捷键

    1.进入Mysql的安装目录bin文件夹下 如默认路径: cd C:\Program Files\MySQL\MySQL Server 8.0\bin 2.本地登录MySQL 命令:mysql -u ...

  6. 深入浅出 Java Concurrency (22): 并发容器 part 7 可阻塞的BlockingQueue (2)[转]

    在上一节中详细分析了LinkedBlockingQueue 的实现原理.实现一个可扩展的队列通常有两种方式:一种方式就像LinkedBlockingQueue一样使用链表,也就是每一个元素带有下一个元 ...

  7. Java 如何在线打开编辑word文档?

    在一般的OA项目中经常会遇到在线处理Office文档的需求,先下载文件,编辑保存后再选择文件上传的方式太过原始,在如今早已是Office Online的时代,没有用户能接受这种蹩脚的操作方式. 虽然微 ...

  8. 警告: [SetPropertiesRule]{Context/Loader} Setting property 'useSystemClassLoaderAsParent' to 'false' did not find a matching property.

    警告: [SetPropertiesRule]{Context/Loader} Setting property 'useSystemClassLoaderAsParent' to 'false' d ...

  9. 【linux配置】Redhat6.5基础配置指南

    Redhat6.5基础配置指南 本文针对鄙人在工作中常用系统配置加以说明,通常公司用于生产和测试的服务器基本上都不能连接外网,需要针对刚刚安装好的系统做一系列的常用配置. 一.常用基础系统配置 1.关 ...

  10. mysql 无法存储joda time的datetime类型

    com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect datetime value: '\xAC\xED\x00\x05sr\x ...