leetcode 【 Merge Two Sorted Lists 】 python 实现
题目:
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
代码:oj在线测试通过 Runtime: 208 ms
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
# @param two ListNodes
# @return a ListNode
def mergeTwoLists(self, l1, l2):
if l1 is None:
return l2
if l2 is None:
return l1 dummyhead = ListNode(0)
dummyhead.next = None
p = dummyhead
while l1 is not None and l2 is not None:
if l1.val > l2.val:
p.next = l2
l2 = l2.next
else:
p.next = l1
l1 = l1.next
p = p.next
if l1 is not None:
p.next = l1
else:
p.next = l2
return dummyhead.next
思路:
虚表头dummyhead设定好了就不要动,设定一个p=hummyhead,然后从处理p.next开始;最后返回hummyhead就可以获得正确的结果
两个表的指针一步步移动,并通过比较val的大小决定哪个插入p.next
注意,每次执行完判断,要移动指针p=p.next
leetcode 【 Merge Two Sorted Lists 】 python 实现的更多相关文章
- leetcode Merge K sorted Lists python
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...
- leetcode Merge Two Sorted Lists python
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...
- [LeetCode] Merge k Sorted Lists 合并k个有序链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 这 ...
- [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 ...
- 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 ...
- LeetCode: Merge k Sorted Lists 解题报告
Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...
- [Leetcode] Merge k sorted lists 合并k个已排序的链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 思 ...
- 21. Merge Two Sorted Lists —— Python
题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...
- LeetCode:Merge k Sorted Lists
题目链接 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexi ...
- LeetCode——Merge k Sorted Lists
Discription: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its ...
随机推荐
- Java中调用MatLab返回值
当在Java中使用MatLab函数时,由于语言语法的不同,Matlab返回多个数据时,想在Java中获取到并进行使用.查阅了网上资料,翻箱倒柜加上自己实战,得出方法如下: 如MatLab函数返回的是N ...
- Uva 12299 带循环移动的RMQ(线段树)
题目链接:https://vjudge.net/contest/147973#problem/C 题意:传统的RMQ是一个不变的数组a求区间最值.现在要循环移动(往前移动). 分析:求区间问题,很容易 ...
- 百度非会员满速下载利器(IDM)Internet Download Manager v6.30.8 中文特别版
下载利器(IDM)Internet Download Manager v6.30.8 中文特别版 所属分类:工具软件 应用平台:Windows 资源版本:v6.30.8 最后更新:2018年04月14 ...
- selenium+chrome下载文件,格式怎么选择???
学习了下载 if browser == "Chrome": options=webdriver.ChromeOptions() prefs={'profile.default_co ...
- 2017.9.16 Web 应用开发环境搭建与开发工具安装
1.JDK的下载与安装 1.1 在网址:http://javase/downloads/index.jsp网站下载最新的JDK版本 1.2 安装jdk,双击下载好的.exe文件运行,一般默认安装在c盘 ...
- python-一切事物都是对象
python:一切事物都是对象 开始接触python,在里面有一句话“一切事物都是对象”,那么如何来理解这句话呢,下面举简单的例子: a=1 b='hello't=(11,22,33) list1=[ ...
- 2.1-Java语言基础(keyword)
2.1 keyword watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbXNpcmVuZQ==/font/5a6L5L2T/fontsize/400/fi ...
- JavaEE权限管理系统的搭建(三)--------springmvc和mabatis整合环境搭建
本节介绍如何环境的搭建和配置: 首先要在父工程引入jar包依赖: <!-- 通过属性定义指定jar的版本 --> <properties> <spring.version ...
- 旧文备份:在CANopen网络中通过LSS服务设置节点地址和网络波特率
CANopen专有个子协议用来描述怎样去通过网络设置节点地址和波特率,就是CiA DSP-305,大伙都叫LSS协议,是Layer Setting Services的缩写,不太好翻译,可以叫底层设置服 ...
- js中的变量提升(Hoisting)
<script> function test(){ console.log(a); console.log(foo()); var a=1; function foo(){ return ...