leetcode解题报告(10):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.
分析
有序链表的合并以前在数据结构课上就上过了,但是现在写起来不是很顺手,可能是因为太久没接触了,当然,整体思路还是很清晰的: )
对于输入的两个链表,如果其中一个为空,那么输出另外一个链表的头结点即可。
否则做如下处理:
初始化新建的链表的头结点。比较l1和l2的头结点的元素大小,若l1->val < l2->val,则让头结点指向l1,同时让l1指向下一元素,否则指向l2.
令结点p指向头结点head,只要l1和l2均不为空,则比较l1和l2当前结点的大小,若l1的结点元素小于l2,则让p的下一结点指向l1,同时l1指向下一结点。l2同理。
每一次处理完后,都要更新p的值,即让p指向下一结点。
若其中一个结点为空,则退出循环,让p的下一结点指向不为空的链表即可。最后,返回头结点head。
/**
* 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)return l2;
if(!l2)return l1;
ListNode*head = NULL;
if(l1->val < l2->val){
head = l1;
l1 = l1->next;
}else{
head = l2;
l2 = l2->next;
}
ListNode *p = head;
while(l1 && l2){
if(l1->val < l2->val){
p->next = l1;
l1 = l1->next;
}else{
p->next = l2;
l2 = l2->next;
}
p = p->next;
}
p->next = l1 ? l1 : l2;
return head;
}
};
另一解法如下:
https://discuss.leetcode.com/topic/6187/14-line-clean-c-solution
该解法也是先初始化链表,只不过这一步是通过链表的构造函数来完成的。初始化后,再定义一个链表指针,让它指向该链表的头结点,之后的步骤就和上面的解法一样了。
/**
* 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) {
ListNode dummy(INT_MIN);
ListNode* tail = &dummy;
while(l1 && l2){
if(l1->val < l2->val){
tail->next = l1;
l1 = l1->next;
}else{
tail->next = l2;
l2 = l2->next;
}
tail = tail->next;
}
tail->next = l1?l1:l2;
return dummy.next;
}
};
leetcode解题报告(10):Merge Two Sorted Lists的更多相关文章
- 【LeetCode算法-21】Merge Two Sorted Lists
LeetCode第21题 Merge two sorted linked lists and return it as a new list. The new list should be made ...
- LeetCode之“链表”:Merge Two Sorted Lists && Merge k Sorted Lists
1. Merge Two Sorted Lists 题目链接 题目要求: Merge two sorted linked lists and return it as a new list. The ...
- leetCode练题——21. Merge Two Sorted Lists(照搬大神做法)
1.题目 21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new l ...
- leetcode第22题--Merge k Sorted Lists
problem:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its compl ...
- [LeetCode&Python] Problem 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
算法和数据结构这东西,真的是需要常用常练.这道看似简单的链表合并题,难了我好几个小时,最后还是上网搜索了一种不错算法.后期复习完链表的知识我会将我自己的实现代理贴上. 这个算法巧就巧在用了递归的思想, ...
- LeetCode(23)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 splicin ...
- LeetCode解题报告—— Search in Rotated Sorted Array & Search for a Range & Valid Sudoku
1. Search in Rotated Sorted Array Suppose an array sorted in ascending order is rotated(轮流,循环) at so ...
- LeetCode解题报告—— Median of Two Sorted Arrays
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
随机推荐
- Centos 安装 graylog
安装文档 安装nginx做反向代理 (如果生产环境通过端口管理则用不到) 通过yum安装方便以后升级 yum install nginx chkconfig nginx on service ngin ...
- Java中编码问题
在开发过程中经常会遇到一会乱码问题,不是什么大问题,但是也挺烦人的,今天来将我们开发总结的经验记录下来,希望可以给大家一些帮助. 一些概念: 字符:人们使用的记号,抽象意义上的一个符号.比如:‘1’, ...
- [Vue]axios 发出请求的config
这些是用于发出请求的可用配置选项. 只有url是必需的. 如果未指定方法,请求将默认为GET. { // `url`是将用于请求的服务器URL url: '/user', // `method`是发出 ...
- VS App_Code文件夹下的类文件不能直接被调用的解决方法
如下图所示,新建的类不能直接使用,会显示报错,检查命名空间什么的,未果 通过百度搜索,发现这么一篇文章:https://blog.csdn.net/younghaiqing/article/detai ...
- c#基础知识梳理(三)
上期回顾 - https://www.cnblogs.com/liu-jinxin/p/10824638.html 一.方法 一个方法是把一些相关的语句组织在一起,用来执行一个任务的语句块.每一个 C ...
- JS基础_构造函数修改
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- sudo pip3找不到命令
转自: https://blog.csdn.net/Cryhelyxx/article/details/53384004 编辑/etc/sudoers 找到Defaults env_reset, 将其 ...
- CSS过渡效果transition和动画
一.过渡效果 可以在不适用Flash和js 的情况下实现过渡效果 属性 描述 transition 简写属性,用于在一个属性中设置四个过渡属性 transition-property 规定应用过渡的c ...
- linux命令启动关闭firewalld防火墙,添加端口
firewalld管理防火墙常用命令 1.查看防火墙的状态 [root@localhost HMK]# firewall-cmd --state 查看防火墙的运行状态 not running [r ...
- javascript 给事件任务一个缓冲区
在编写前端的过程中,经常会监听事件并执行任务,我在这抛出2个比较常见的场景: 1.输入关键字搜索如果你监听input的chage事件,会有一个问题,在使用中文输入法时,你输入的几个拼音字母都会被触发我 ...