题目:

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. go 通过select实现超时

    package main import ( "fmt" "time" ) func main() { ch := make(chan int) quit := ...

  2. [PHP] Laravel5.5 使用 laravel-cors 实现 Laravel 的跨域配置

    Laravel5.5  使用 laravel-cors 实现 Laravel 的跨域配置 最开始的时候,我使用的是路由中间件的方式,但是发现中间件不起作用 这是之前使用的方式: 'cros' => ...

  3. Educational Codeforces Round 63 (Rated for Div. 2) E 带模高斯消元

    https://codeforces.com/contest/1155/problem/E 题意 \(f(x)=a_0+a_1x+a_2x^2+...+a_kx^k,k \leq 10,0 \leq ...

  4. 第02组Beta版本演示

    组长博客 组名:十一个憨比 本组组员: 学号 姓名 分工 贡献比例 181700413 黄智 写Beta冲刺的四次博客,写评审表,写word,统筹规划 9% 131700309 林闽沪 代码实现,答辩 ...

  5. 58同城笔试题:数组去重;分饼干(分糖果);最小路径和(leetcode64)

    1. 数组去重 题目描述 /** * 有序数组去重 * 输出最终的数字个数 * 输入:1,2,2 * 输出:2 * @author Turing * */ 代码 import java.util.*; ...

  6. 关于ios 11.X后微信wifi认证,无法打开微信,无法重定向到weixin:开头网址等问题的处理

    环境: 认证路由ROS ,认证后台python django ios11系统 更新以来先后出现微信wifi认证,无法打开微信,无法重定向到weixin:开头网址等相关问题. 经过问题的收集,查询到网络 ...

  7. ubuntu18.04下安装无线网卡驱动心得

    联想Lenovo的笔记本,装完系统wifi显示找不到适配器. lspci | grep Wireless 显示无线网卡类型为博通的BCM43162. 网上一查,果然有问题. apt install f ...

  8. Asp.Net Core 中的静态文件

    Asp.Net Core 中的静态文件 在这节中我们将讨论如何使 ASP.NET Core 应用程序,支持静态文件,如 HTML,图像,CSS 和 JavaScript 文件. 静态文件 默认情况下, ...

  9. 基于WEB的网上购物系统-ssh源码

    基于WEB的网上购物系统主要功能包括:前台用户登录退出.注册.在线购物.修改个人信息.后台商品管理等等.本系统结构如下:(1)商品浏览模块:        实现浏览最新商品        实现按商品名 ...

  10. JavaScript RegExp(正则表达式) 对象

    正则表达式是描述字符模式的对象.正则表达式用于在文本上执行模式匹配和“搜索和替换”功能. var patt = /JC2182/i 示例说明: /JC2182/i - 是一个正则表达式. JC2182 ...