题目:

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

分析:

将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。

创建一个新的节点head,比较了l1和l2的值的大小,将较小的加入到head,依次比较,如果l1,l2有剩余,就接在后面即可。

l1         l2                              head,p

↓           ↓            ↓

1->2->4      1->3->4                     0

l1         l2                              head p

↓          ↓           ↓   ↓

2->4       1->3->4                     0->1

l1         l2                              head    p

↓          ↓          ↓    ↓

2->4          3->4                          0->1->1

l1         l2                              head        p

↓          ↓          ↓      ↓

4               3->4                          0->1->1->2

l1         l2                              head             p

↓          ↓          ↓        ↓

4                4                              0->1->1->2->3

l1         l2                              head                    p

↓          ↓          ↓              ↓

null            4                               0->1->1->2->3->4

l1         l2                              head                        p

↓          ↓          ↓                   ↓

null            null                           0->1->1->2->3->4->4

最后返回head.next即可。

还可以递归求解此问题。

mergeTwoLists( l1, l2) //l1:1->2->4,l2:1->3->4

=>{1} + mergeTwoLists( l1, l2) //l1:2->4,l2:1->3->4

=>{1->1} + mergeTwoLists( l1, l2) //l1:2->4,l2:3->4

=>{1->1->2} + mergeTwoLists( l1, l2) //l1:4,l2:3->4

=>{1->1->2->3} + mergeTwoLists( l1, l2) //l1:4,l2:4

=>{1->1->2->3->4} + mergeTwoLists( l1, l2) //l1,l2:4

=>{1->1->2->3->4->4}

程序:

/**
* 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) {
ListNode head();
ListNode* p = &head;
while(l1 && l2){
if(l1->val < l2->val){
p->next = l1;
l1 = l1->next;
}
else{
p->next = l2;
l2 = l2->next;
}
p = p->next;
}
if(l1) p->next = l1;
if(l2) p->next = l2;
return head.next;
}
};
//递归
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if(l1 == nullptr) return l2;
if(l2 == nullptr) return l1;
if(l1->val < l2->val){
l1->next = mergeTwoLists(l1->next, l2);
return l1;
}
else{
l2->next = mergeTwoLists(l1, l2->next);
return l2;
}
}
};

LeetCode 21. Merge Two Sorted Lists合并两个有序链表 (C++)的更多相关文章

  1. leetcode 21 Merge Two Sorted Lists 合并两个有序链表

    描述: 合并两个有序链表. 解决: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { if (!l1) return l2; if (!l2) ...

  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】21. Merge Two Sorted Lists 合并两个有序链表

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,有序链表,递归,迭代,题解,leetcode, 力 ...

  4. 【LeetCode】Merge Two Sorted Lists(合并两个有序链表)

    这道题是LeetCode里的第21道题. 题目描述: 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1-&g ...

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

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

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

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

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

随机推荐

  1. NOIP模拟赛 最佳组合

    题目描述 Description \(Bzeroth\) 大陆最终还是覆灭了,所以你需要为地灾军团服务了. 地灾军团军师黑袍不擅长写题面,所以你只需要看简化版的题意即可. 给定 \(3\) 个长度均为 ...

  2. 游戏引擎架构 (Jason Gregory 著)

    第一部分 基础 第1章 导论 (已看) 第2章 专业工具 (已看) 第3章 游戏软件工程基础 (已看) 第4章 游戏所需的三维数学 (已看) 第二部分 低阶引擎系统 第5章 游戏支持系统 (已看) 第 ...

  3. Token认证,如何快速方便获取用户信息

    背景 我们有一个Web项目,这个项目提供了很多的Rest API.也做了权限控制,访问API的请求必须要带上事先认证后获取的Token才可以. 认证的话就在Filter中进行的,会获取请求的Token ...

  4. 第02组 Beta冲刺(2/5)

    队名:無駄無駄 组长博客 作业博客 组员情况 张越洋 过去两天完成了哪些任务 数据库实践 提交记录(全组共用) 接下来的计划 加快校园百科的进度 还剩下哪些任务 学习软工的理论课 学习代码评估.测试 ...

  5. 剑指offer:序列化二叉(前序遍历+层次)

    1. 题目描述 /** 请实现两个函数,分别用来序列化和反序列化二叉树 二叉树的序列化是指:把一棵二叉树按照某种遍历方式的结果以某种格式保存为字符串,从而使得内存中建立起来的二叉树可以持久保存. 序列 ...

  6. Flink是如何实现exactly-once语义的

    转自:https://blog.csdn.net/xianpanjia4616/article/details/86375224 最少一次:断了之后 重新执行 再去重 严格一次:根据检查点,再执行一次 ...

  7. git commit 提交失败

    git commit -m 'xxx' 报错 报错信息 当前分支:master 远程分支:gitlib.xxx error: cannot spawn .git/hooks/commit-msg: N ...

  8. kali渗透综合靶机(十一)--BSides-Vancouver靶机

    kali渗透综合靶机(十一)--BSides-Vancouver靶机 靶机下载地址:https://pan.baidu.com/s/1s2ajnWHNVS_NZfnAjGpEvw 一.主机发现 1.n ...

  9. Greenplum集群或者Postgresql出现死锁肿么办?

    1.Greenplum集群或者Postgresql出现死锁肿么办? 由于Postgresql和Greenplum集群这数据库知识很深的,没有仔细研究,遇到问题真的不知道肿么处理,我遇到死锁,是采取了暴 ...

  10. C 函数指针、回调函数

    参考链接:https://www.runoob.com/cprogramming/c-fun-pointer-callback.html 函数指针 函数指针就是执行函数的指针,他可以像正常函数一样去调 ...