LeetCode21 Merge Two Sorted Lists
题意:
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. (Easy)
分析:
链表题,注意细节即可。
链表merge和数组merge的不同在于链表不用拷一份出来,倒腾一下指针就可以啦。
注意事项有:
1. dummy node用于输出,head(或者换个名字)用于遍历;
2. l1,l2是不是为空,一个结束遍历之后记得把另一个剩下的加上去。
代码:
 class Solution {
 public:
     ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
         if (l1 == nullptr) {
             return l2;
         }
         if (l2 == nullptr) {
             return l1;
         }
         ListNode dummy();
         ListNode* head = &dummy;
         while (l1 != nullptr && l2 != nullptr) {
             if (l1 -> val < l2 -> val) {
                 head -> next = l1;
                 head = head -> next;
                 l1 = l1 -> next;
             }
             else {
                 head -> next = l2;
                 head = head -> next;
                 l2 = l2 -> next;
             }
         }
         if (l1 != nullptr) {
             head -> next = l1;
         }
         if (l2 != nullptr) {
             head -> next = l2;
         }
         return dummy.next;
     }
 };
优化一下:
head = head -> next是不论if 还是else都要做的,拿出来写;
按本题的写法和ListNode的定义,其实开始不用判断l1,l2是否为空。(但一般来讲还是写上为好...)
代码:
 class Solution {
 public:
     ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
         ListNode dummy();
         ListNode* head = &dummy;
         while (l1 != nullptr && l2 != nullptr) {
             if (l1 -> val < l2 -> val) {
                 head -> next = l1;
                 l1 = l1 -> next;
             }
             else {
                 head -> next = l2;
                 l2 = l2 -> next;
             }
             head = head -> next;
         }
         if (l1 != nullptr) {
             head -> next = l1;
         }
         if (l2 != nullptr) {
             head -> next = l2;
         }
         return dummy.next;
     }
 };
今天七夕,算是半个假期,状态不是很好,水一道链表题练练手啦。明天开始重新步入正轨按照题号刷啦!!!
LeetCode21 Merge Two Sorted Lists的更多相关文章
- LeetCode 21. 合并两个有序链表(Merge Two Sorted Lists)
		
21. 合并两个有序链表 21. Merge Two Sorted Lists 题目描述 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. LeetCode ...
 - [LeetCode] Merge k Sorted Lists 合并k个有序链表
		
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 这 ...
 - [LeetCode] 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 ...
 - [LintCode] Merge Two Sorted Lists 混合插入有序链表
		
Merge two sorted (ascending) linked lists and return it as a new sorted list. The new sorted list sh ...
 - No.023:Merge k Sorted Lists
		
问题: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexit ...
 - Merge k Sorted Lists
		
1. Merge Two Sorted Lists 我们先来看这个 问题: Merge two sorted linked lists and return it as a new list. The ...
 - 71. Merge k Sorted Lists
		
Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...
 - 【leetcode】Merge k Sorted Lists
		
Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...
 - Merge Two Sorted Lists
		
Merge Two Sorted Lists https://leetcode.com/problems/merge-two-sorted-lists/ Merge two sorted linked ...
 
随机推荐
- jmeter是什么
			
Apache JMeter 是Apache 组织开发的基于 Java 的压力测试工具: 适用的测试领域:地方 用于对软件做压力测试,它可以用于测试静态和动态资源,例如:静态文件,Java 小程序.CG ...
 - 用惯了jquery, 想用angularjs 还真不好理解
			
jquery 比较直白,什么都是操作dom 节点. angularjs 就好比 thinkphp, ci 等框架,有自己约定的格式和方式.需要遵循它的规则,研究中... 比如说我,用了很长事件的jqu ...
 - lc287 Game of Live
			
lc287 Game of Live 难点在于不能使用额外的空间. 观察到数组中每一个元素要么为1要么为0,32位int只用了一位,可以利用bit操作,将第二次state存储到int变量的倒数第二位中 ...
 - MySQL系列(八)--数据库分库分表
			
在互联网公司或者一些并发量比较大的项目,虽然有各种项目架构设计.NoSQL.MQ.ES等解决比较高的并发访问,但是对于数据库来说,压力 还是太大,这时候即使数据库架构.表结构.索引等都设计的很好了,但 ...
 - 阿里云应用高可用服务 AHAS 流控降级实现 SQL 自动防护功能
			
在影响系统稳定性的各种因素中,慢 SQL 是相对比较致命的,可能会导致 CPU.LOAD 异常.系统资源耗尽.线上生产环境出现慢 SQL 往往有很多原因: 硬件问题.如网络速度慢,内存不足,I/O 吞 ...
 - 海量大数据大屏分析展示一步到位:DataWorks数据服务+MaxCompute Lightning对接DataV最佳实践
			
1. 概述 数据服务(https://ds-cn-shanghai.data.aliyun.com) 是DataWorks产品家族的一员,提供了快速将数据表生成API的能力,通过可视化的向导,一分钟“ ...
 - 【html、CSS、javascript-12】jquery-效果
			
一.jQuery 效果- 隐藏和显示 通过 jQuery,您可以使用 hide() 和 show() 方法来隐藏和显示 HTML 元素: $("#hide").click(func ...
 - Thinkphp [美味]常用代码
			
//调试开关 function _initialize () { // 调试开关 C ( 'SHOW_PAGE_TRACE', TRUE ); } //判断 IS_AJAX && $t ...
 - 惊!VUE居然数据不能驱动视图?$set详细教程
			
众所周知.VUE最大的优点就是数据驱动视图.当数据发生改变时,会监听到变化,后渲染到页面上.那么为什么当我们在修改data中声明的数组或对象时.VUE并没有监听到变化呢?这个我也不知道.我们可以后续再 ...
 - mysql与hibernate选择某个字段的最大值,比如表中的最大id
			
hibernate public int getMaxId(Session session) { String hql = "SELECT MAX(id) FROM ArticleModel ...