题意:

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的更多相关文章

  1. LeetCode 21. 合并两个有序链表(Merge Two Sorted Lists)

    21. 合并两个有序链表 21. Merge Two Sorted Lists 题目描述 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. LeetCode ...

  2. [LeetCode] Merge k Sorted Lists 合并k个有序链表

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 这 ...

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

  4. [LintCode] Merge Two Sorted Lists 混合插入有序链表

    Merge two sorted (ascending) linked lists and return it as a new sorted list. The new sorted list sh ...

  5. No.023:Merge k Sorted Lists

    问题: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexit ...

  6. Merge k Sorted Lists

    1. Merge Two Sorted Lists 我们先来看这个 问题: Merge two sorted linked lists and return it as a new list. The ...

  7. 71. Merge k Sorted Lists

    Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...

  8. 【leetcode】Merge k Sorted Lists

    Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...

  9. Merge Two Sorted Lists

    Merge Two Sorted Lists https://leetcode.com/problems/merge-two-sorted-lists/ Merge two sorted linked ...

随机推荐

  1. python学习笔记3_数据载入、存储及文件格式

    一.丛mysql数据库中读取数据 import pandas as pdimport pymysqlconn = pymysql.connect( host = '***', user = '***' ...

  2. python编程:从入门到实践学习笔记

    python编程:从入门到实践学习笔记 原文地址:https://blog.csdn.net/qq_35554125/article/details/79548192 [day 1]python编程: ...

  3. jquery校验是否为空

    function lang(key) { mylang = { 'ls_input_myb': '请输入您的账户', 'ls_myb_email': '漫游币账户为邮箱地址', 'ls_login_p ...

  4. JVM学习-之对象的创建和内存分配

    最近看JVM内存模型,看了很多文章,大都讲到JVM将内存区域划分分:Mehtod-Area(No heap) 方法区,Heap(堆)区,Program Counter Register(程序计数器), ...

  5. 修改代码150万行!与 Blink 合并后的 Apache Flink 1.9.0 究竟有哪些重大变更?

    8月22日,Apache Flink 1.9.0 正式发布,早在今年1月,阿里便宣布将内部过去几年打磨的大数据处理引擎Blink进行开源并向 Apache Flink 贡献代码.当前 Flink 1. ...

  6. 移动端页面-点击input输入框禁止放大效果

    点击input输入框会获取焦点并且放大 解决方法:在项目根目录找到 index.html <meta name="viewport" content="width= ...

  7. 【笔记】LR11中关联设置

    LR中关联建议都手动进行,自动不好用,也容易出错. 在LR中我们什么要做关联:1.关联解决的是动态数据的参数化.2.关联的数据一定是服务器响应的数据.3.服务器响应过来的数据在后面的服务还要使用. 手 ...

  8. @import vs #import - iOS 7

    It's a new feature called Modules or "semantic import". There's more info in the WWDC 2013 ...

  9. shell总结:读取文件、参数、if、分割字符串、数组长度、空文件、变量赋值、多进程、按行切割文件、查看线程

    Reference: http://saiyaren.iteye.com/blog/1943207 1.     Shell  读取文件和写文件 for line in $(<top30000. ...

  10. html中有序列表标签ol,li的高级应用

    本文主要介绍html中有序列表标签ol,li的高级应用, 在网页设计时我们设计有序列表内容时,经常会在每个ITEM前手工加上一个数值,或是由程序加上这个数值. 而如果使用有序列表标签ol和li,则不需 ...