题目:

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;
}
}
};

最新文章

  1. Android 坐标系和 MotionEvent 分析、滑动
  2. Python 学习笔记三
  3. 蛋疼的Fedora17
  4. Nginx基础知识之————什么是 Nginx?
  5. 【Cocos2d实例教程一】xcode5下Cocos2d环境的搭建
  6. DOS命令 Net config server Net config workstation
  7. UVA 705 Slash Maze
  8. 源代码jar包中中文注释乱码
  9. Android常用控件之Fragment仿Android4.0设置界面
  10. 最短路径算法——Dijkstra算法
  11. bootstrap快速入门笔记(三)响应式,行,列,偏移量,排序
  12. Java伪代码描述《大道至简》第一章
  13. [Codeforces]849E Goodbye Souvenir
  14. JS 中的广度与深度优先遍历
  15. mysql in 查询参数化
  16. 胖哈勃杯Pwn400、Pwn500详解
  17. 几句话的事儿,LogBack急速使用
  18. head first--------------state pattern
  19. css细节复习笔记——内边距、边框和外边距
  20. 「美团 CodeM 资格赛」跳格子

热门文章

  1. nodejs 连接MySQL后,输出数据带有RowDataPacket、中括号大括号怎么去掉?
  2. 关于webpack的面试题
  3. UOJ Easy Round #5
  4. python threading ThreadPoolExecutor
  5. 【java提高】---patchca生成验证码
  6. 使用WebApi和Asp.Net Core Identity 认证 Blazor WebAssembly(Blazor客户端应用)
  7. mac下安装php7.2、mysql5.7、nginx环境
  8. vue中路由传值url--路径传值
  9. memory一致性模型
  10. SAP S4HANA 账户组的配置里&#39;Int.Std.Grping&#39;选项没勾选导致ABAP程序报错