[leetcode] 17. 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.
就是算导里面的第一个算法讲解,但是我的C++水平实在太渣,所以对于链表的操作很蠢,但还好完成了。题解如下:
/**
* 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 && l2 == NULL)
{
return NULL;
} if (l1 == NULL || l2 == NULL)
{
return (l1 == NULL) ? (l2) : (l1);
}
ListNode *aspros, *deferos, *root;
aspros = deferos = new ListNode(0);
if (l1->val <= l2->val)
{
aspros->val = l1->val;
aspros->next = NULL;
l1 = l1->next;
}
else
{
aspros->val = l2->val;
aspros->next = NULL;
l2 = l2->next;
}
root = aspros; while (l1 && l2)
{
if (l1->val <= l2->val)
{
aspros = new ListNode(0);
aspros->val = l1->val;
aspros->next = NULL;
deferos->next = aspros;
deferos = aspros;
l1 = l1->next;
}
else
{
aspros = new ListNode(0);
aspros->val = l2->val;
aspros->next = NULL;
deferos->next = aspros;
deferos = aspros;
l2 = l2->next;
}
} while (l1)
{
aspros = new ListNode(0);
aspros->val = l1->val;
aspros->next = NULL;
deferos->next = aspros;
deferos = aspros;
l1 = l1->next;
} while (l2)
{
aspros = new ListNode(0);
aspros->val = l2->val;
aspros->next = NULL;
deferos->next = aspros;
deferos = aspros;
l2 = l2->next;
} return root;
}
};
这个就是因为我不会初始化链表,所以在一开始的地方非常蠢,作了两个判断,一个是l1与l2都为NULL的情况,另一个是它俩有一个是空的情况,可以直接弹出,接着就是合并了。
[leetcode] 17. Merge Two Sorted Lists的更多相关文章
- Java for LeetCode 023 Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解 ...
- [LeetCode] 21. 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 ...
- 蜗牛慢慢爬 LeetCode 23. Merge k Sorted Lists [Difficulty: Hard]
题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...
- LeetCode 23 Merge k Sorted Lists(合并k个有序链表)
题目链接: https://leetcode.com/problems/merge-k-sorted-lists/?tab=Description Problem: 给出k个有序的list, 将其进行 ...
- [Leetcode Week4]Merge Two Sorted Lists
Merge Two Sorted Lists题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/merge-two-sorted-lists/descrip ...
- [LeetCode] 21. 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 ...
- [LeetCode] 23. Merge k Sorted Lists 合并k个有序链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. E ...
- 【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 23.Merge Two Sorted Lists Merge K Sorted Lists
Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list shoul ...
随机推荐
- Linux下php5.3.3安装mcrypt扩展
具体操作: 一.下载软件包 1.下载php(版本要与系统安装的一致) http://pan.baidu.com/s/1mifTbfE 2.下载libmcrypt(安装mcrypt需要此软件包) htt ...
- General error 2006 MySQL server has gone away
写入配置文件的办法: max_allowed_packet = 16M //但是这种有时候不支持,1024*1024*16这种有的也不支持 max_allowed_packet = 16777216 ...
- samtools软件的使用
1)samtools简介--------------------------------------------------------------------------背景:前面我们讲过sam/b ...
- 在Ubuntu上安装微信
1) 从https://github.com/geeeeeeeeek/electronic-wechat/releases地址中下载linux-x64.tar.gz文件到/opt/wechat文件夹 ...
- wireshark使用相关问题
问题1: 打开wireshark,没有出现过滤器 解决1: 使用管理员方式登录 过滤: http and ip.src == 192.168.0.10 and ip.dst == 192.168.0. ...
- 60. Permutation Sequence (String; Math)
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- xcode - 显示安装过的低版本模拟器
1. 更改版本
- html注释快捷键
1.选中需要注释的内容--->ctrl+shift+/ 2.取消注释--->ctrl+shift+\
- metasploit渗透测试指南概要整理
一.名词解释 exploit 测试者利用它来攻击一个系统,程序,或服务,以获得开发者意料之外的结果.常见的 有内存溢出,网站程序漏洞利用,配置错误exploit. payload 我们想让被攻击系统执 ...
- tomcat用虚拟目录方式发布项目与manager页面配置
conf/Catalina/localhost:指定项目的配置信息 1.添加:ROOT.xml 听见Context节点: <Context docBase="/usr/local/to ...