描述

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的更多相关文章

  1. 【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 ...

  2. 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 ...

  3. 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 ...

  4. leetcode第22题--Merge k Sorted Lists

    problem:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its compl ...

  5. [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 ...

  6. LeetCode记录之21——Merge Two Sorted Lists

    算法和数据结构这东西,真的是需要常用常练.这道看似简单的链表合并题,难了我好几个小时,最后还是上网搜索了一种不错算法.后期复习完链表的知识我会将我自己的实现代理贴上. 这个算法巧就巧在用了递归的思想, ...

  7. LeetCode(23)Merge k Sorted Lists

    题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...

  8. 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 ...

  9. 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 ...

  10. 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 ...

随机推荐

  1. //统计报表-供水量统计主列表分页查询 Element-ui的分页插件

    <!-- //分页 --> <div class="pagination">时间(月) <el-pagination @current-change= ...

  2. 入手线段树 hdu1754

    今天学习了线段树的三个基本操作 建树 更新 查找 先理解下什么是线段树就这个题目而言 如果我们用普通的数组去存放 然后依次遍历访问的话 时间太多了线段树利用了二分的思想 把数据以段的形式进行储存 这样 ...

  3. 【爬虫集合】Python爬虫

    一.爬虫学习教程 1. https://www.jianshu.com/u/c32d557edfa3 2. WebMagic是一个简单灵活的Java爬虫框架.基于WebMagic,你可以快速开发出一个 ...

  4. (九)SpringBoot之使用jsp

    一.概念 jsp应该尽量避免使用,原因如下: jsp只能打包为:war格式,不支持jar格式,只能在标准的容器里面跑(tomcat,jetty都可以) 内嵌的Jetty目前不支持JSPs Undert ...

  5. Spring Boot 项目集成 Alibaba Druid

    Druid 是一个非常好用的数据库连接池,但是他的好并不止体现在作为一个连接池加快数据访问性能上和连接管理上,他带有一个强大的监控工具:Druid Monitor.不仅可以监控数据源和慢查询,还可以监 ...

  6. Scientific Toolworks Understand for linux

    Scientific Toolworks Understand for linux 这个软件我找了很久了,一直没有找到合适能装的.现在这款能在linux上顺利运行的版本,共享给需要的TX们. 个人觉得 ...

  7. 分布式事务(ACID特性、CAP定律)

    普通事务和分布式事务的区别: 普通事务就是一般所说的数据库事务,事务是数据库管理系统执行过程中的一个逻辑单位,由一个有限的数据库操作序列构成.当事务被提交给了DBMS(数据库管理系统),则DBMS(数 ...

  8. 如何自定义starter

    在springboot启动流程的系列文章中,我们看过了springboot的自动配置机制,本文将基于自动配置机制自定义一个自动配置的starter示例 正文 模块结构 首先,我们准备两个模块servi ...

  9. elementUI动态数据表格(带分页)

    index.vue <template> <div> <el-table ref="multipleTable" :data="tableD ...

  10. Async await 解析

    Async 定义:使异步函数以同步函数的形式书写(Generator函数语法糖) 原理:将Generator函数和自动执行器spawn包装在一个函数里 形式:将Generator函数的*替换成asyn ...