题意:

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

思路:

  设合并后的链表为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. hdu 1576 A/B (求逆元)

    题目链接 Problem Description 要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973) = 1).   Inpu ...

  2. TMF SID性能相关实体介绍

    TMF SID性能相关实体介绍 Copyright © TeleManagement Forum 2013. All Rights Reserved. This document and transl ...

  3. Java Script 学习笔记 -- Ajax

    AJAX 一 AJAX预备知识:json进阶 1.1 什么是JSON? JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.JSON是用字符串来表示Javas ...

  4. InnoDB信息说明

    InnoDB是MySQL数据库发展至今一款至关重要的数据库存储引擎,其不仅支持事务特性,并且具有丰富的统计信息,便于数据库管理人员了解最近InnoDB存储引擎的运行状态. 早期版本的InnoDB存储引 ...

  5. JS中的for....in循环 和 for ...of循环以及iterable遍历Map和Set

    for循环的一个变体是for ... in循环,它可以把一个对象的所有属性依次循环出来: var o = { name: 'Jack', age: 20, city: 'Beijing' }; for ...

  6. Mysql导入导出数据库11111

    导出: 通过命令行  在MYSQL中的bin文件夹的目录下 输入:D:\phpStudy\MySQL\bin>mysqldump -uroot -p 数据库名 > 导出的文件名 导入: 需 ...

  7. Mail.Ru Cup 2018 Round 2C(__gcd)

    #include<bits/stdc++.h>using namespace std;long long mx(long long l1,long long r1,long long l2 ...

  8. elasticsearch 聚合查询

    1. 按照 tags 字段 进行分组 GET /ecommerce/product/_search{ "size": 0,  "aggs": {    &quo ...

  9. dbms_xplan的display_cursor查看执行计划

    准备工作: SQL> conn sys/root as sysdba Connected. SQL> grant select on v_$sql_plan to scott; Grant ...

  10. docker-compose搭建wordpress[转]

    1.安装docker-compose apt-get install docker-compose 发现下载的是旧版本,不支持2.0的配置文件 还是下载新版本吧,去github查看最新版本https: ...