描述

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. Struts2连接Mysql的Crud使用

    今天分享的是struts2框架中增删改查的用法: 一:利用Struts2框架 1.1在pom.xml中导入相关依赖 <project xmlns="http://maven.apach ...

  2. Qt中的常用容器类(解释比较全面,有插图)

    在Qt库中为我们提供了一系列的基于模板的容器类.这些类可以被用来存储特定类型的项.例如,如果你需要一个大小可以变得QString数组,那么可以使用QVector<QString>. 这些容 ...

  3. (二)easyUI之消息提示框

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  4. C#特性 详解

    一:Conditional:条件特性,预定义了一个条件方法. 使用方法: [Conditional("DEBUG")] public void test() { MessageBo ...

  5. 配置vue 多页面

    安装vue 1. 全局安装 vue-cli环境 npm install --global vue-cli 2. 创建一个基于webpack模板的新项目 vue init webpack my-proj ...

  6. Mac下安装和配置Vue项目

    题记:学vue有一段时间了,终于今天下定决心每日书写一篇学习笔记.1.访问node.js官网:https://nodejs.org/en/ 下载对应安装包.2.安装完成,在终端输入 : node -v ...

  7. 第三方应用如何在SAP Kyma上进行服务注册

    Jerry之前的公众号文章 什么?在SAP中国研究院里还需要会PHP开发?提到了一个SAP Kyma的应用场景: 旅行兼社交达人伊森,使用经过SAP Kyma扩展之后的WordPress这个网站来写博 ...

  8. Ubuntu-Python2.7安装 scipy,numpy,matplotlib (转)

    sudo apt-get install python-scipy sudo apt-get install python-numpy sudo apt-get install python-matp ...

  9. B+树Java代码实现以及测试

    M阶B+树的定义: 任意非叶子结点最多有M个子节点:且M>2: 除根结点以外的非叶子结点至少有 M/2个子节点: 根结点至少有2个子节点: 除根节点外每个结点存放至少M/2和至多M个关键字:(至 ...

  10. 2.原子变量 CAS算法

    前面提到,使用volatile无法保证 变量状态的原子性操作,所谓原子性,就是不可再分 如:i++的原子性问题,i++ 的操作实际上分为三个步骤  "读-改-写" (1)保存i的值 ...