题目:

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. ESP8266 AT指令开发(基于STC89C52单片机): 测试下诱人的程序(51单片机,8266,MQTT远程通信控制)

    前言 实现的功能,APP通过SmartConfig给Wi-Fi模块配网并绑定设备,然后通过MQTT远程控制开发板的继电器, 简而言之: 51单片机+ESP8266用AT指令实现实现MQTT,(连接的本 ...

  2. Protobuf 文件导入和生成

    build: protoc -I=$(GOPATH)/pkg/mod/github.com/micro/micro@v1.13.1/api/proto -I /Users/lzy/Git/Learn/ ...

  3. 剑指offer:二叉搜索树的第k个结点(中序遍历)

    1. 题目描述 /* 给定一棵二叉搜索树,请找出其中的第k小的结点. 例如, (5,3,7,2,4,6,8) 中,按结点数值大小顺序第三小结点的值为4. */ 2. 思路 中序遍历二叉搜索树,第K个就 ...

  4. 在线web软件

    编程 Compiler Explorer SharpLab 着色器 shadertoy 腾讯文档 word  excel  ppt ... 思维导图 MindMaster ProcessOn 流程图. ...

  5. 【新特性速递】优化Shift/Ctrl行多选逻辑,和Windows的文件资源管理器保持一致!

    别告诉我你不知道 别告诉我你不知道可以使用键盘的 Shift 和 Ctrl 来多选表格行,因为这个在 Windows 文件资源管理器中的常用操作,此时的画风是这样的: 这个动图中进行了如下操作: 1. ...

  6. LeetCode 155:最小栈 Min Stack

    LeetCode 155:最小栈 Min Stack 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- ...

  7. 应用层内存溢出/越界/重复释放等问题检查工具(ASan)

    https://github.com/google/sanitizers/wiki https://github.com/google/sanitizers/wiki/AddressSanitizer ...

  8. MySQL数据库中查询表的所有列名

    MySQL数据库中: 查询某个数据库中某个表的所有列名 SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = ...

  9. Python中的passed by assignment与.NET中的passing by reference、passing by value

    Python文档中有一段话: Remember that arguments are passed by assignment in Python. Since assignment just cre ...

  10. sql server 下载安装标记

    SQL Server 2017 的各版本和支持的功能 https://docs.microsoft.com/zh-cn/sql/sql-server/editions-and-components-o ...