题意:

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

思路:

  设合并后的链表为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. Java基础 -- 泛型之泛型参数

    泛型机制常用的参数有3个: “?”代表任意类型.如果只指定了<?>,而没有extends,则默认是允许任意类. extends关键字声明了类型的上界,表示参数化的类型可能是所指定的类型,或 ...

  2. App集成极光推送开发流程[关键步骤]

    1.客户端集成SDK 1.1初始化 JPushInterface.setDebugMode(true); // 设置开启日志,发布时请关闭日志 JPushInterface.init(this); / ...

  3. android build system resource links

    总体结构,参见这里:http://www.jayway.com/2012/10/24/a-practical-approach-to-the-aosp-build-system/ 一般应用的Andro ...

  4. C#中类和结构体

    结构体 类 自己的一些理解 首先结构中不能给字段赋值  而类可以 结构调用方法是  例如 People p1: 类的调用方法是  Book b =new Book(): 1.类能够实例化 而结构不可以 ...

  5. sqlserver2012——.Net

    1.Connection 属性: ConnectionString:获取或者设置用于打开SQLServer数据库的字符串 Database:获取当前数据库或者连接打开后要使用的数据库名称 State: ...

  6. 第三章 Goroutine调度策略(16)

    本文是<Go语言调度器源代码情景分析>系列的第16篇,也是第三章<Goroutine调度策略>的第1小节. 在调度器概述一节我们提到过,所谓的goroutine调度,是指程序代 ...

  7. PHP连接 redis

    <?php //连接本地的 Redis 服务 $redis = new Redis(); //连接redis 地址 端口 连接超时时间 连接成功返回true 失败返回false $redis-& ...

  8. 厉害了,Dubbo 正式毕业!

    厉害了,2019/05/21 Apache软件基金会发表博文,宣布 Dubbo 在 2019/05/20 这天正式毕业,成为 Apache 的顶级项目. 参考:https://blogs.apache ...

  9. 小程序组件交互 -- 传入js

    1.父组件(wxml页面)向子组件传递 在子组件中定义需要传入的属性 properties: { count: { type: Number, value: 0, observer:function( ...

  10. 配置了SSH后还是每次都要求输入密码

    保存凭证可以解决问题 git config --global credential.helper store