[LeetCode]题解(python):021-Merge Two Sorted Lists
题目来源:
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的更多相关文章
- [Leetcode][Python]21: Merge Two Sorted Lists
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 21: Merge Two Sorted Listshttps://oj.le ...
- [Leetcode][Python]23: Merge k Sorted Lists
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 23: Merge k Sorted Listshttps://oj.leet ...
- 【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 ...
- 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 ...
- 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 ...
- 【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 ...
- 【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 ...
- 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 ...
- 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 ...
- leetcode第22题--Merge k Sorted Lists
problem:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its compl ...
随机推荐
- css hr 设置
http://www.sovavsiti.cz/css/hr.html http://adamculpepper.net/blog/css/hr-tag-css-styling-cross-brows ...
- ListView 行高设置不起作用
通常我们在自定义listview的时候会遇到这个问题,我们设置了每行的layout的高度,但是没起作用,那是因为在adapter里面getview的时候出的问题,通常我们写: view = mInfl ...
- 如何从 0 开始学 ruby on rails (漫步版)
如何从 0 开始学 ruby on rails (漫步版) ruby 是一门编程语言,ruby on rails 是 ruby 的一个 web 框架,简称 rails. 有很多人对 rails 感兴 ...
- swith 语句详解
switch 语句的格式: switch ( 整型或字符型变量 ) { case 变量可能值1 : 分支一; break; case 变量可能值2 : 分支二; break; case 变量可 ...
- android技术牛人的博客[转]
Android+JNI调用–文件操作 开发环境:Windows xp sp3 +MyEclipse 8.6+android2.3.3+jdk1.6+android-ndk-r6b JNI概述: ...
- openGl学习之加入颜色
OpenGL 支持两种颜色模式:一种是 RGBA模式.一种是 颜色索引模式. 不管哪种颜色模式.计算机都必须为每个像素保存一些数据,即通过每个像素的颜色,来改变总体图形的颜色.不同的是. RGBA 模 ...
- BZOJ 1266: [AHOI2006]上学路线route(最短路+最小割)
第一问最短路.第二问,先把最短路的图建出来(边(u,v)满足d[s->u]+d[v->t]+d(u,v)==最短路径长度,就在图中,可以从源点和汇点分别跑一次最短路得到每个点到源点和汇点的 ...
- Hibernate学习之延迟加载
转自:http://www.cnblogs.com/xiaoluo501395377/p/3371776.html 在hibernate中我们知道如果要从数据库中得到一个对象,通常有两种方式,一种是通 ...
- servlet三种实现方式之二继承GenericServlet开发
servlet有三种实现方式: 1.实现servlet接口 2.继承GenericServlet 3.通过继承HttpServlet开发servlet 第二种示例代码如下(已去掉包名): //这是第二 ...
- 10247 - Complete Tree Labeling(递推高精度)
Problem B Complete Tree Labeling! Input: standard input Output: standard output Time Limit: 45 secon ...