题目来源:

  https://leetcode.com/problems/merge-two-sorted-lists/


题意分析:

  题目给出两个排好序的链表,将这两个链表整合成一个新的有序的链表。


题目思路:

  这道题目很简单,首先构造一个新的链表,比较两个链表的指针指向的节点的值大小,将值较少的节点放到新链表中,并将指针位置往后移动一位,直到某个链表为空之后,把剩下的链表全部放到新的链表中就可以了。时间复杂度是(O(m+n))


代码(python):

 # 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
"""
ans = ListNode(0)
tmp = ans
if l1 == None and l2 == None:
return None
while l1 !=None or l2 != None:
if l1 == None:
while l2 != None:
tmp.val = l2.val
l2 = l2.next
if l2 == None:
break
tmp.next = ListNode(0)
tmp = tmp.next
break
if l2 == None:
while l1 != None:
tmp.val = l1.val
l1 = l1.next
if l1 == None:
break
tmp.next = ListNode(0)
tmp = tmp.next
break
if l1.val <= l2.val:
tmp.val = l1.val
l1 = l1.next
else:
tmp.val = l2.val
l2 = l2.next
tmp.next = ListNode(0)
tmp = tmp.next
return ans

转载请注明出处:http://www.cnblogs.com/chruny/p/4872779.html

[LeetCode]题解(python):021-Merge Two Sorted Lists的更多相关文章

  1. [Leetcode][Python]21: Merge Two Sorted Lists

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 21: Merge Two Sorted Listshttps://oj.le ...

  2. [Leetcode][Python]23: Merge k Sorted Lists

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 23: Merge k Sorted Listshttps://oj.leet ...

  3. 【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 ...

  4. 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 ...

  5. 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 ...

  6. 【LeetCode】021. 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 ...

  7. 【JAVA、C++】LeetCode 021 Merge Two Sorted Lists

      Merge two sorted linked lists and return it as a new list. The new list should be made by splicing ...

  8. Leetcode 021 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. 021 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 ...

  10. leetcode第22题--Merge k Sorted Lists

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

随机推荐

  1. ProFTPD 初探

    ProFTPD:一个Unix平台上或是类Unix平台上(如Linux, FreeBSD等)的FTP服务器程序.

  2. JAVA排序(一) Comparable接口

    昨天接到一个实习公司的电话面试,来的很突然,没有准备. 由于以前没用过,在被他问及是否用过JAVA的排序工具Comparable与Comparator时,没有回答上来,只能实话实说没有用过. 感觉太丢 ...

  3. 发送请求工具—Advanced REST Client

    Advanced REST Client是Chrome浏览器下的一个插件,通过它能够发送http.https.WebSocket请求.在Chrome商店下搜索Advanced REST Client, ...

  4. C# 动态载入Dll

    1.新建測试dll及方法,用vs2010新建winform程序,详细代码例如以下: using System; using System.Collections.Generic; using Syst ...

  5. 百度地图API 级别自动缩放

    今天做一个基于百度地图API的小项目 查了很长时间apid都没有找到地图呈现出来的时候地图按坐标的多少自动缩放显示的等级比例,特此记录笔记!var points = [point1, point2,p ...

  6. offsetParent 到底是哪一个?

    前言 温故而知新.遇到offsetParent这个知识点,发现书上讲的不够详细.于是看了看豪情的博客,发现讲得很具体,收藏一下. 正文 不同情况 没有已定位的父节点,且自身position:relat ...

  7. sharepoint 自定义字段实现省市联动

    最后实现效果如下:设置栏如下:解决方案结构如下: fldtypes_RoyCustomField.xml 内容如下: <?xml version="1.0" encoding ...

  8. android http同步请求

    1.界面 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:too ...

  9. iOS 自定义各类bar的属性

    在iOS应用开发中,经常需要为导航栏和标签栏设置相同的主题,一个一个去设置的话,就太麻烦了,可以通过对应用中所有的导航栏和标签栏同意设置背景.字体等属性. 如:创建一个继承自“UINavigation ...

  10. 我的IOS学习之路(三):手势识别器

    在iOS的学习中,对于手势的处理是极为重要的,如对于图片,我们经常需要进行旋转,缩放以及移动等.这里做一下总结,详见代码. - (void)viewDidLoad { [super viewDidLo ...