描述

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. docker 实践四:仓库管理

    本篇我们来了解 docker 仓库的内容. 注:环境为 CentOS7,docker 19.03 仓库(Responsitory)是集中存放镜像的地方,又分公共仓库和私有仓库. 注:有时候容易把仓库与 ...

  2. Golang不会自动把slice转换成interface{}类型的slice

    目录 例子 原因 如何去实现 例子 我们时常会写一些interface,例如: type A interface{ Print() } type B struct { } func (b *B) Pr ...

  3. Gossip协议

    Gossip数据传播协议: Fabric通过将工作负载划分到事务执行(背书和提交)对等节点和事务排序节点,优化了区块链网络性能.安全性和可伸缩性.这种网络操作的解耦需要一个安全.可靠和可伸缩的数据传播 ...

  4. hdu 4496 其实还是并查集

    Problem Description Luxer is a really bad guy. He destroys everything he met. One day Luxer went to ...

  5. android 项目集成 微信支付

    0.环境 下载 libammsdk.jar 1.需要的常量值 public class Constant { /** 微信中用到的常量值 */ public static final class WX ...

  6. Node初始以及环境搭建(Node01)

    1. 相关概念 •什么是JavaScript? •一种遵守ECMAScript标准的脚本语言 •最初只能运行在浏览器端 •浏览器中的 JavaScript 可以做什么? •操作DOM:表单验证.动画 ...

  7. 使用ef core自动生成mysql表和数据编码的问题

    mysql默认的编码是不支持中文的,需要改成utf8编码格式. 而我使用的Pomelo.EntityFrameworkCore.MySql组件生成mysql库和表,他是使用默认编码的. 网上大多说修改 ...

  8. Tomcat server.xml port server context 配置

  9. 使用Java Executor框架实现多线程

    本文将涵盖两个主题: 通过实现Callable接口创建线程 在Java中使用Executor框架 实现Callable接口 为了创建一段可以在线程中运行的代码,我们创建了一个类,然后实现了Callab ...

  10. C#基础 结构体、枚举

    一 结构体 结构体(struct)指的是一种数据结构,一个变量组,是一个自定义的集合.通常使用结构体创造新的“属性”,封装一些属性来组成新的类型.   结构体一般定义在Mian函数上面,位于Class ...