第六周leetcode题
来源:互联网 发布:饿了么商家版for mac 编辑:程序博客网 时间:2024/05/01 22:24
description:
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.
将两个已经排好序的数组合并为一个,因为两个数列已经排好序,所以只需要分别顺序检索两个数列,将其中较小者放入新的数列中即可。注意要考虑空数列的情况。
代码如下:
class Solution {
public:ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
ListNode re(0);
ListNode * head = &re;
while(1)
{
if(l1 == NULL && l2 == NULL)break;
if(l1 == NULL)
{
head->next = l2;
l2 = l2->next;
}
else if(l2 == NULL)
{
head->next = l1;
l1 = l1->next;
}
else if(l1->val < l2->val)
{
head->next = l1;
l1 = l1->next;
}
else{
head->next= l2;
l2 = l2->next;
}
head = head->next;
}
return re.next;
}
};
0 0
- 第六周leetcode题
- leetcode-python 第六周
- 第六周LeetCode
- 第六周LeetCode
- [leetcode] 第六周作业
- LeetCode第六周
- 第六周LeetCode
- LeetCode题解 第六周
- leetcode第六周解题报告
- Leetcode 算法习题 第六周
- 第六周LeetCode算法题两道
- Leetcode 算法习题 第六周
- LeetCode第六题,ZigZag Conversion
- Leetcode第六题_ZigZag Conversion
- leetcode 第六题 ZigZag Conversion
- 第六周:[Leetcode]127. Word Ladder
- 第六周:[Leetcode]100. Same Tree
- 第六周:[Leetcode]112. Path Sum
- 继承
- Linux--UDPsocket文件传输
- hadoop streaming python 处理 lzo 文件遇到的问题
- 1139: 输出最短字符串
- docker 安装部署及常用命令
- 第六周leetcode题
- C#基础-继承、多态与接口
- bzoj3144 [Hnoi2013]切糕
- elasticsearch单个索引添加字段
- CDOJ 31 饭卡(card)及HDOJ 2546 饭卡
- 截获室友的梦话
- 《c++primer》笔记 第1章 开始
- C#特性详解
- 蓝桥 灾后重建