【Leetcode】【Easy】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.
解题:
题目比较简单,注意由于表头不确定,因此新建一个结点指向新创建的链表;
解题步骤:
1、判断l1和l2是否有效
2、新建preHead结点,newlist指针指向preHead;
3、循环开始,满足l1和l2都不为空:
(1)比较当前l1和l2的值,将较小的穿进newlist中
4、newlist链接l1或l2的剩余部分
5、释放表头,返回newlist;
/**
* 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) {
if (l1 == NULL) return l2;
if (l2 == NULL) return l1; ListNode* prehead = new ListNode();
ListNode* newlist = prehead; while (l1 != NULL && l2 != NULL) {
if (l1->val > l2->val) {
newlist->next = l2;
l2 = l2->next;
} else {
newlist->next = l1;
l1 = l1->next;
}
newlist = newlist->next;
} if (l1 == NULL) newlist->next = l2;
else newlist->next = l1; l1 = prehead->next;
delete prehead;
return l1;
}
};
【Leetcode】【Easy】Merge Two Sorted Lists .的更多相关文章
- LeetCode Linked List Easy 21. Merge Two Sorted Lists
Description Merge two sorted linked lists and return it as a new list. The new list should be made b ...
- C# 写 LeetCode easy #21 Merge Two Sorted Lists
21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list s ...
- Leetcode练习题21. Merge Two Sorted Lists
题目描述(easy) Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new ...
- 21. Merge Two Sorted Lists【easy】
21. Merge Two Sorted Lists[easy] Merge two sorted linked lists and return it as a new list. The new ...
- 【LeetCode】21. Merge Two Sorted Lists 合并两个有序链表
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,有序链表,递归,迭代,题解,leetcode, 力 ...
- 【LeetCode】23. Merge k Sorted Lists 合并K个升序链表
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,链表,单链表,题解,leetcode, 力扣,Py ...
- 【leetcode】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 ...
- 【LeetCode】【数组归并】Merge k Sorted Lists
描述 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
随机推荐
- 在循环中使用continue语句
continue;//表示继续,当遇到continue语句时,则结束当次循环继续执行下一次循环 例子: class For09{ public static void main(String[ ]a ...
- oracle 中的exists 和 in 效率问题
oracle中的 exists 和 in 的效率问题 --------------------------------------------------------------- +++++++++ ...
- Python 如何计算当前时间减少或增加一个月
问题 今天在之前的代码中发现了一个bug,有个计算当前时间减少一个月的函数,其报出下面的异常信息: ValueError: day is out of range for month 看一下代码: i ...
- java ee的map
- bzoj 3064: Tyvj 1518 CPU监控
Description 1.区间加 \(z\) 2.区间覆盖为 \(z\) 3.查询区间最大值 4.查询区间历史最大值 Solution 线段树维护历史最值,思想大致是维护标记出现过的最大值 考虑这种 ...
- java 配置在.properties文件中的常量
不让用常量类,那就用.properties文件配置,放在根目录. import java.util.HashMap; import java.util.Iterator; import java.ut ...
- jQuery ajax async
jQuery 同步调用: jQuery.ajax({ type:'POST', async: false, url:'qcTask/add', contentType:'application/jso ...
- sql server存储过程中SELECT 与 SET 对变量赋值的区别 转自Theo
SQL Server 中对已经定义的变量赋值的方式用两种,分别是 SET 和 SELECT. 对于这两种方式的区别,SQL Server 联机丛书中已经有详细的说明,但很多时候我们 并没有注意,其实这 ...
- Centos 从零开始 (四)
12:nginx架设 rtmp直播 首先在之前 所安装的nginx是用 yum 源安装的.却不能为nginx添加他的模块 所以要用wget来下载源文件 来进行源码配置与安装.开搞 [root@loca ...
- Swift强制解析
IDE:Xcode Version7.3.1 Swift中"数据类型?"表示这是可选类型,即 某个常量或者变量可能是一个类型,也可能什么都没有,不确定它是否有值,也许会是nil. ...