描述

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. JS 05 json

    1.下载jar包: https://www.github.com/alibaba/fastjson/releases https://www.mvnrepository.com/artifact/co ...

  2. AtCoder Grand Contest 040 A - ><

    传送门 对于某个位置,只要知道这个位置往左最多的连续 $\text{<}$ 的数量 $x$ 和往右最多的连续 $\text{>}$ 的数量 $y$ 那么这个位置最小可能的数即为 $max( ...

  3. 基于Docker的Kafka部署

    一 准备 1.1 安装docker-dompose #部署 sudo curl -L "https://github.com/docker/compose/releases/download ...

  4. [JZOJ5279]香港记者题解--最短路图

    [JZOJ5279]香港记者题解--最短路图 题目链接 过 于 暴 力 分析 有一个naiive的想法就是从1到n跑最短路,中途建图,然后在图上按字典序最小走一遍,然而·这是不行的,你这样跳不一定能跳 ...

  5. Python练习_Python初识_day1

    题目 1.作业 1.简述变量命名规范 2.name = input(“>>>”) name变量是什么数据类型? 3.if条件语句的基本结构? 4.用print打印出下面内容: ⽂能提 ...

  6. windows找不到头文件的问题

    windows系统中,设置好了环境变量,就可以在cmd下直接执行文件,但是 特别是在c语言或者c++程序中,include头文件的问题,如果找不到,就考虑是不是文件放错地方了. windows上编译c ...

  7. ubuntu创建kvm虚拟机

    CPU虚拟化支持 [root@ubuntu~]# egrep -o '(vmx|svm)' /proc/cpuinfo vmx vmx vmx vmx KVM环境 [root@ubuntu ~]# a ...

  8. swoole| swoole 协程初体验 转

    swoole| swoole 协程初体验   date: 2018-5-30 14:31:38title: swoole| swoole 协程初体验description: 通过协程的执行初窥 swo ...

  9. 自定义flask转换器

    自定义flask转换器 以匹配手机号为例: # 1. 定义自己的转换器 class MobileConverter(BaseConverter): def __init__(self, url_map ...

  10. vuejs开发流程

    https://www.cnblogs.com/yexiaowang/p/8489250.html