题意:

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. 【MFC 】关于对话框中的OnVScroll() 和 OnHScroll

    原文地址:[MFC 中]关于对话框中的OnVScroll() 和 OnHScroll()函数作者:Winters     对话框中的滑块,微调控件都会向OnVScroll() 和OnHScroll() ...

  2. HDFS常用Java API

  3. LINUX查询登录主机的用户工具:w 、who 、users

    w.who和users工具,是查询已登录当前主机的用户:另外finger -s 也同样能查询:侧重点不一样:请自己对比着看:毕竟简单,这里只是介绍 : [beinan@localhost ~]$ w ...

  4. stream分组

    1.根据集合元素中的一个属性值分组 Person p1 = new Person("张三", new BigDecimal("10.0"));Person p2 ...

  5. 【html、CSS、javascript-11】jquery-事件使用方法总结

    jquery提供了许多的事件处理函数,下面对其总结一下,梳理一下知识点,便于记忆和使用. 一.鼠标事件 1. click():鼠标单击事件 $div = $("div") $div ...

  6. PHP拓展 - xhprof性能分析工具

    Windows安装 参考:https://www.cnblogs.com/buexplain/p/4821619.html dll文件下载:https://windows.php.net/downlo ...

  7. 前端插件--swipe.js

    swipe.js的作用: 这是一个轻量级的移动滑动组件,支持触摸移动,支持响应式页面. 效果图: 代码: <!DOCTYPE html> <html lang="en&qu ...

  8. Axure 工具的使用

    Axure RP是一款专业的快速原型设计工具,Axure RP是美国Axure Software Solution公司旗舰产品,是一个专业的快速原型设计工具,让负责定义需求和规格.设计功能和界面的专家 ...

  9. @import vs #import - iOS 7

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

  10. hdu 1166 敌兵布阵(线段树区间求和)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...