题意:

  将两个有序的链表归并为一个有序的链表。

思路:

  设合并后的链表为head,现每次要往head中加入一个元素,该元素要么属于L1,要么属于L2,可想而知,此元素只能是L1或者L2的首个元素,那么进行一次比较就可以知道是谁了。操作到L1或L2其中一个已经没有元素为止,剩下的直接加到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 *start=,*end=;
if(l1->val<l2->val){
start=end=l1;
l1=l1->next;
}
else{
start=end=l2;
l2=l2->next;
}
while(l1!=&&l2!=){
if(l1->val<l2->val){
end->next=l1;
l1=l1->next;
}
else{
end->next=l2;
l2=l2->next;
}
end=end->next;
}
if(l1==)
end->next=l2;
else
end->next=l1;
return start;
}
};

AC代码

python3

 # Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None class Solution(object):
def mergeTwoLists(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
head=cur=ListNode(0)
while l1 and l2:
if l1.val<l2.val: cur.next, l1=l1, l1.next
else: cur.next, l2=l2, l2.next
cur=cur.next
if l1: cur.next=l1
if l2: cur.next=l2
return head.next

AC代码

LeetCode Merge Two Sorted Lists 归并排序的更多相关文章

  1. [LeetCode] Merge k Sorted Lists 合并k个有序链表

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

  2. [LeetCode] 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 ...

  3. LeetCode: Merge k Sorted Lists 解题报告

    Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...

  4. [Leetcode] Merge k sorted lists 合并k个已排序的链表

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

  5. LeetCode: Merge Two Sorted Lists 解题报告

    Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list shoul ...

  6. LeetCode——Merge k Sorted Lists

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

  7. LeetCode Merge k Sorted Lists (链表)

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

  8. [Leetcode] 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 ...

  9. LeetCode:Merge k Sorted Lists

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

随机推荐

  1. linux 的 scp 命令

    linux 的 scp 命令 可以 在 linux 之间复制 文件 和 目录: ================== scp 命令 ================== scp 可以在 2个 linu ...

  2. 《Linux内核设计与实现》读书笔记(二)- 内核开发的准备

    在尝试内核开发之前,需要对内核有个整体的了解. 主要内容: 获取内核源码 内核源码的结构 编译内核的方法 内核开发的特点 1. 获取内核源码 内核是开源的,所有获取源码特别方便,参照以下的网址,可以通 ...

  3. 【mysql远程连库】

    mysql连接远程库: 服务器端: 1.登陆服务器端,进入命令行,windows cmd; 2.设置用户.密码让指定的IP访问:MySQL -u root -p 或安装的快捷方式进入:MySQL Co ...

  4. 【mybatis 如何写union和union查询】

    select d.* from (select a.CheckType,b.UserName,a.CheckNumber, a.PayName ,a.PayBank,a.PayBankNumber,a ...

  5. 【C#】VS2017 winform 打包

    首先要在想要打包的项目下创建一个新的项目, 创建好setup项目,之后点击属性,去修改打包软件的名字,ProductName....可以选填 到此已经创建好了setup工程了,那么下面开始将要打包的d ...

  6. Docker:容器与主机时间不同步问题解决

    在Docker容器运行后,可能会发现容器时间与宿主机时间不一致,一般会差8个小时.这样会造成在容器中运行的web程序打出的日志时间与实际时间不一致,如果web程序中有定时任务也会造成影响等,需要对宿主 ...

  7. C - Trailing Zeroes (III)(二分)

    You task is to find minimal natural number N, so that N! contains exactly Q zeroes on the trail in d ...

  8. linux系统elementray os的环境搭建

    因为我在使用过程中为了改变终端的外表,结果把/ect/psswd,以及/ect/profile中的文件配置修改之后,我把gnome-terminal的python脚本打包放在/bin/目录下,修改了/ ...

  9. 二维偏序 tree

    tree(二维偏序) 最近接触到一些偏序的东西. 传统线段树非叶子节点的划分点mid=(l+r)/2,但小R线段树mid是自己定的.但满足l<=mid<r,其余条件同原来线段树.那么不难发 ...

  10. Filter的使用及其生命周期介绍

    一.Filter 1. Filter简介 > Filter翻译为中文是过滤器的意思. > Filter是JavaWeb的三大web组件之一:Servlet.Filter.Listener ...