[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 ...
随机推荐
- 解决libc.so.6: version `GLIBC_2.14' not found问题, 升级glibc,glibc-2.15
0.以下在系统CentOS 6.3 x86_64上操作 1.试图运行程序,提示"libc.so.6: version `GLIBC_2.14' not found",原因是系统的g ...
- ubuntu-12.04.4-server安装
一.系统ISO下载 下载地址:http://www.ubuntu.com/download 根据自己的需求下载,我的电脑配置一般,因此选择32位的. 二.虚拟机配置 ...
- 网页解析不了PHP源代码的解决方法
一般出现这个错误都是因为修改了apache配置文件,但是没有使apache配置生效. 所以只要执行以下命令就行了: sudo apachectl restart sudo apachectl grac ...
- 利用KVC实现无需协议的委托模式
在<精通iOS开发>一书中看到的技巧.假设BIDTaskListController是一个列表,点击列表上的一项将会导航到BIDTaskDetailController,在BIDTaskD ...
- 编译器DIY——词法分析
在上一篇文章中已经介绍了读文件的操作,那么这一篇文章中将会细致解释词法分析. 在源文件里解析出的单词流必须识别为保留字,标识符,常量,操作符和界符五大类 1.显然我们须要列举出全部的保留字,而这里与保 ...
- BZOJ 1878: [SDOI2009]HH的项链( BIT )
离线处理 , 记下询问的左右端点并排序 , 然后可以利用树状数组 , 保证查询区间时每种颜色只计算一次 ------------------------------------------------ ...
- [LeetCode]题解(python):070-Climbing Stairs
题目来源: https://leetcode.com/problems/climbing-stairs/ 题意分析: 爬楼梯,一次可以爬一步或者两步.如果要爬n层,问一共有多少种爬法.比如说,如果是3 ...
- web api 开发之 filter
1.使用filter之前应该知道的(不知道也无所谓,哈哈!) 谈到filter 不得不先了解下aop(Aspect Oriented Programming)面向切面的编程.(度娘上关于aop一大堆 ...
- tomcat应用转到weblogic上时的问题
昨天将一个tomcat环境下调试通过的报表demo应用发布到weblogic上做测试,结果发现好多问题.总结了一下,主要有这么几点: 1.使用log4j的问题. tomcat应用直接发布到weblog ...
- C#共享内存类改进版
原文 C#共享内存类改进版 改进说明及源码实例下载见:http://blog.csdn.net/zzh8845/archive/2008/11/22/3349963.aspx ShareMem.cs ...