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;
} if (l1 -> val <= l2 -> val){
l1 -> next = mergeTwoLists(l1 -> next, l2);
return l1;
}
else{
l2 -> next = mergeTwoLists(l1, l2 -> next);
return l2;
} }
};

非递归实现:

/**
* 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(); // 新建一个结点,不能是指向空的指针,否则它的指向不会改变
ListNode * cur = &head;
while (l1 && l2){
if (l1 -> val <= l2 -> val){
cur -> next = l1;
l1 = l1 -> next;
}else{
cur -> next = l2;
l2 = l2 -> next;
}
cur = cur -> next;
}
if (l1){
cur -> next = l1;
}
if (l2){
cur -> next = l2;
}
return head.next; // 注意head此时为一个实体对象,不是一个指针,所以要用.来指示next.为什是next??
}
};

Leetcode 21. Merge Two Sorted Lists(easy)的更多相关文章

  1. 【leetcode】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 ...

  2. LeetCode:21. Merge Two Sorted Lists(Easy)

    1. 原题链接 https://leetcode.com/problems/merge-two-sorted-lists/description/ 2. 题目要求 给出两个已经从小到大排序的链表ls1 ...

  3. 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 ...

  4. # 蜗牛慢慢爬 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 ...

  5. 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 ...

  6. LeetCode 21 Merge Two Sorted Lists (有序两个链表整合)

    题目链接 https://leetcode.com/problems/merge-two-sorted-lists/?tab=Description   Problem: 已知两个有序链表(链表中的数 ...

  7. LeetCode 21. Merge Two Sorted Lists(c++)

    要定义两个链表 判断时依次对应每一个链表的值进行判断即可. /** * Definition for singly-linked list. * struct ListNode { * int val ...

  8. LeetCode 21. Merge Two Sorted Lists(合并两个有序链表)

    题意:合并两个有序链表 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next ...

  9. 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 ...

随机推荐

  1. 【Zabbix】zabbix设置邮件报警

    目录 Zabbix设置邮件报警 1.安装sendmail或postfix 2.安装邮件发送工具mailx . 3.配置mail 4. 测试邮件发送 5.编写邮件发送脚本sendmail.sh 6.设置 ...

  2. 第43章 添加更多API端点 - Identity Server 4 中文文档(v1.0.0)

    您可以向托管IdentityServer4的应用程序添加更多API端点. 您通常希望通过它们所托管的IdentityServer实例来保护这些API.这不是问题.只需将令牌验证处理程序添加到主机(请参 ...

  3. 原生js及H5模拟鼠标点击拖拽

    一.原生js 1.拖拽的流程动作 鼠标按下 触发onmousedown事件 鼠标移动 触发onmousemove事件 鼠标松开 触发onmouseup事件 2.注意事项: 要防止div移出可视框,要限 ...

  4. es6的let,const

    1.es6 新增的let const 命令 let用来定义一个局部变量,故名思意就是只在当前代码块可用 1.1 let 声明的变量不存在变量提升(var 声明的变量存在变量提升)且代码块内 暂时性死区 ...

  5. 如何开启红米手机4X的ROOT超级权限

    红米手机4X通过什么方法拥有了root权限?大家都清楚,Android机器有root权限,如果手机拥有了root相关权限,可以实现更强的功能,举个栗子大家公司的营销部门同事,使用大多数营销软件都需要在 ...

  6. (办公)SpringBoot与mybatisGenerator自动生成.

    20181206-自动生成,少写一点代码. (以下的内容主要参考csdn上的<[完美]SpringBoot+Mybatis-Generator自动生成>这篇文章,还有简书上的mbatis- ...

  7. Vue一个案例引发「内容分发slot」的最全总结

    今天我们继续来说说 Vue,目前一直在自学 Vue 然后也开始做一个项目实战,我一直认为在实战中去发现问题然后解决问题的学习方式是最好的,所以我在学习一些 Vue 的理论之后,就开始自己利用业余时间做 ...

  8. SQL Server -- 回忆笔记(三):ADO.NET之C#操作数据库

    SQL Server知识点回忆篇(三):ADO.NET之C#操作数据库 1.连接数据库 (1)创建连接字符串: 使用windows身份验证时的连接字符串: private string conStr= ...

  9. Hive的命名空间

    Hive的命名空间分为:hiveconf , system, env 和 hivevar 1.hiveconf 的命名空间指的是hive-site.xml下面配置的环境变量 2.system的命名空间 ...

  10. windows环境下安装yaf框架

    windows环境下安装yaf框架 在windows下安装yaf框架 准备工作: php环境(过程略,wamp,xampp,phpstudy都行,php版本大于5.3) git工具(需要从github ...