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 list should be made by splicing together the nodes of the first two lists.
简单题,只要对两个链表中的元素进行比较,然后移动即可,只要对链表的增删操作熟悉,几分钟就可以写出来,代码如下:
struct ListNode {
int val;
ListNode *next;
ListNode(int x):val(x), next(NULL) {}
};
ListNode *GetLists(int n) //得到一个列表
{
ListNode *l = new ListNode();
ListNode *pre = l;
int val;
for (int i = ; i < n; i ++) {
cin >> val;
ListNode *newNode = new ListNode(val);
pre->next = newNode;
pre = pre->next;
}
return l->next;
}
ListNode *mergeTwoLists(ListNode *l1, ListNode *l2)
{
assert (NULL != l1 && NULL != l2);
if (NULL == l1 && NULL == l2)
return NULL;
if (NULL == l1 && NULL != l2) // !!要记得处理一个为空,另一个不为空的情况
return l2;
if (NULL != l1 && NULL == l2)
return l1;
ListNode *temp = new ListNode();
temp->next = l1;
ListNode *pre = temp;
while(NULL != l1 && NULL != l2) {
if (l1->val > l2->val) { //从小到大排列
ListNode *next = l2->next;
l2->next = pre->next;
pre->next = l2;
l2 = next;
}
else {
l1 = l1->next;
}
pre = pre->next;
}
if (NULL != l2) {
pre->next = l2;
}
return temp->next;
}
这其中要注意一点,即要记得处理一个链表为空,另一个不为空的情况,如{}, {0} -- > {0},当然上面的写法多少啰嗦了一些,可以简写。
LeetCode:21_Merge Two Sorted Lists | 合并两个排序列表 | Easy的更多相关文章
- [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 ...
- Leetcode21--->Merge Two Sorted Lists(合并两个排序的单链表)
题目: 给出两个排序的单链表,合并两个单链表,返回合并后的结果: 解题思路: 解法还是很简单的,但是需要注意以下几点: 1. 如果两个链表都空,则返回null; 2. 如果链表1空,则返回链表2的 ...
- 【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 ...
- 【LeetCode】21. Merge Two Sorted Lists 合并两个有序链表
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,有序链表,递归,迭代,题解,leetcode, 力 ...
- [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 21 Merge Two Sorted Lists 合并两个有序链表
描述: 合并两个有序链表. 解决: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { if (!l1) return l2; if (!l2) ...
- [LeetCode] Merge k Sorted Lists 合并k个有序链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 这 ...
- [Leetcode] Merge k sorted lists 合并k个已排序的链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 思 ...
- Leetcode23--->Merge K sorted Lists(合并k个排序的单链表)
题目: 合并k个排序将k个已排序的链表合并为一个排好序的链表,并分析其时间复杂度 . 解题思路: 类似于归并排序的思想,lists中存放的是多个单链表,将lists的头和尾两个链表合并,放在头,头向后 ...
随机推荐
- CSS 盒子投影
box-shadow 属性可以设置盒子的投影效果.它的原理同文本投影一样.字体风格一节有介绍. 它有4个值,同时使用,也可以有选择地使用: 第一个值 设置阴影左右延伸长度,负值向左,正值向右 第二个值 ...
- 更改Oracle字符集避免乱码
如何更改Oracle字符集避免乱码 转一位大神的笔记. 国内最常用的Oracle字符集ZHS16GBK(GBK 16-bit Simplified Chinese)能够支持繁体中文,并且按照2个字符长 ...
- 十二、Decorator 装饰器模式
设计: 代码清单: Display public abstract class Display { public abstract int getColumns(); public abstract ...
- 数据库类型空间效率探索(五)- decimal/float/double/varchar
以下测试为userinfo增加一列,列类型分别为decimal.float.double.varchar.由于innodb不支持optimize,所以每次测试,都会删除表test.userinfo,重 ...
- CentOS 性能监测命令
1.实时监测命令(watch) -d 高亮显示变化 -n 间隔多久(s) 执行后面的command #每隔1秒显示空间使用情况并列出当前目录下的列表信息 EX:watch -d -n 1 'df -h ...
- 2018 ICPC青岛网络赛 B. Red Black Tree(倍增lca好题)
BaoBao has just found a rooted tree with n vertices and (n-1) weighted edges in his backyard. Among ...
- 信号基础知识--FFT DFT
clc;close all;clear all; f0=10; fs=100; %采样率 t=1/fs:1/fs:2; %共两秒钟,共200个采样点.采样间隔T=1/100 y ...
- [leetcode]17. Letter Combinations of a Phone Number手机键盘的字母组合
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...
- Zabbix安装部署(CentOS系统下)
zabbix(音同 zæbix)是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案. zabbix能监视各种网络参数,保证服务器系统的安全运营:并提供灵活的通知机制以让系统 ...
- stark组件开发之列表页面自定义函数扩展
对于展示页面, 可能需要显示一些. 数据库中,没有的字段. 比如, 删除按钮, 编辑按钮. 这个数据库,是没有的. 所以,可能就需要, 添加一个这个东西. 比如我在渲染的时候, 给他添加两个函数进 ...