[leetcode]Sort List @ Python
原题地址:http://oj.leetcode.com/problems/sort-list/
题意:链表的排序。要求:时间复杂度O(nlogn),空间复杂度O(1)。
解题思路:由于题目对时间复杂度和空间复杂度要求比较高,所以查看了各种解法,最好的解法就是归并排序,由于链表在归并操作时并不需要像数组的归并操作那样分配一个临时数组空间,所以这样就是常数空间复杂度了,当然这里不考虑递归所产生的系统调用的栈。
这里涉及到一个链表常用的操作,即快慢指针的技巧。设置slow和fast指针,开始它们都指向表头,fast每次走两步,slow每次走一步,fast到链表尾部时,slow正好到中间,这样就将链表截为两段。
运行时需要将中文注释删掉,leetcode oj平台里面不支持中文字符。
代码:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
# @param head, a ListNode
# @return a ListNode
def merge(self, head1, head2):
if head1 == None: return head2
if head2 == None: return head1
dummy = ListNode(0) #归并时,新建一个链表头结点
p = dummy
while head1 and head2:
if head1.val <= head2.val:
p.next = head1
head1 = head1.next
p = p.next
else:
p.next = head2
head2 = head2.next
p = p.next
if head1 == None:
p.next = head2
if head2 == None:
p.next = head1
return dummy.next def sortList(self, head):
if head == None or head.next == None:
return head
slow = head; fast = head #快慢指针技巧的运用,用来截断链表。
while fast.next and fast.next.next:
slow = slow.next
fast = fast.next.next
head1 = head
head2 = slow.next
slow.next = None #head1和head2为截为两条链表的表头
head1 = self.sortList(head1)
head2 = self.sortList(head2)
head = self.merge(head1, head2)
return head
[leetcode]Sort List @ Python的更多相关文章
- [leetcode]Sort Colors @ Python
原题地址:https://oj.leetcode.com/problems/sort-colors/ 题意: Given an array with n objects colored red, wh ...
- LeetCode—-Sort List
LeetCode--Sort List Question Sort a linked list in O(n log n) time using constant space complexity. ...
- [LeetCode]题解(python):147-Insertion Sort List
题目来源: https://leetcode.com/problems/insertion-sort-list/ 题意分析: 用插入排序排序一个链表. 题目思路: 这题没什么好说的,直接用插入排序就行 ...
- [leetcode]Insertion Sort List @ Python
原题地址:http://oj.leetcode.com/problems/insertion-sort-list/ 题意:对链表进行插入排序. 解题思路:首先来对插入排序有一个直观的认识,来自维基百科 ...
- [LeetCode]题解(python):090 Subsets II
题目来源 https://leetcode.com/problems/subsets-ii/ Given a collection of integers that might contain dup ...
- [LeetCode]题解(python):078 Subsets
题目来源 https://leetcode.com/problems/subsets/ Given a set of distinct integers, nums, return all possi ...
- [LeetCode]题解(python):049-Groups Anagrams
题目来源 https://leetcode.com/problems/anagrams/ Given an array of strings, group anagrams together. For ...
- [LeetCode]题解(python):047-Permutations II
题目来源 https://leetcode.com/problems/permutations-ii/ Given a collection of numbers that might contain ...
- [LeetCode]题解(python):057-Insert Interval
题目来源 https://leetcode.com/problems/insert-interval/ Given a set of non-overlapping intervals, insert ...
随机推荐
- 码云Android项目构建注意事项(转载)
1.ant项目 build.xml必须位于项目根目录. 2.maven项目 pom.xml必须位于项目根目录. 3.gradle项目 由于gradle的配置灵活,我们做了一些规范,并且增加了一下机制来 ...
- PHP验证时有用的几段代码
1.htmlspecialchars() htmlspecialchars() 函数把一些预定义的字符转换为 HTML 实体.预定义的字符是: & (和号) 成为 & " ( ...
- 洛谷P3265 [JLOI2015]装备购买 [线性基]
题目传送门 装备购买 格式难调,题面就不放了. 分析: 一句话,有$n$件物品,每件物品有$m$个属性和一个花费值,如果一个装备的属性值可以由其他装备的属性值改变系数后组合得到那就不买,求购买最多装备 ...
- 《JAVA软件结构与数据结构》第一周学习总结
学号 20172326 <JAVA软件结构与数据结构>第一周学习总结 教材学习内容总结 软件质量的几大特性 增长函数与大O记法 大O记法用来表示表示增长函数,从而来表示算法的复杂度 算法的 ...
- Android消息总线的演进之路:用LiveDataBus替代RxBus、EventBus
背景 对于Android系统来说,消息传递是最基本的组件,每一个App内的不同页面,不同组件都在进行消息传递.消息传递既可以用于Android四大组件之间的通信,也可用于异步线程和主线程之间的通信.对 ...
- JAVAEE——宜立方商城01:电商行业的背景、商城系统架构、后台工程搭建、SSM框架整合
1. 学习计划 第一天: 1.电商行业的背景. 2.宜立方商城的系统架构 a) 功能介绍 b) 架构讲解 3.工程搭建-后台工程 a) 使用maven搭建工程 b) 使用maven的tomcat插件启 ...
- The Pragmatic Programmer 读书笔记
--在所有的弱点中,最大的弱点就是害怕暴露弱点. --责任是你主动担负的东西.你承诺确保某件事情正确完成,但你不一定能直接控制事情的每一个方面.除了尽你所能以外,你必须分析风险是否超出了你的控制.对于 ...
- 使用SoapUI测试windows身份验证的webservice
有个朋友问到用soapui测试wcf服务时如果使用windows身份验证要怎么传输凭据,于是自己试了一下.其实服务端是wcf还是webservice还是webapi都无所谓,关键是windows身份验 ...
- OpenGL ES 3.0 图元装配
1. 前言 之前已经把纹理的渲染给弄出来了,但是又遇到一个新的问题,那就是图元装配,比如说我已经把图片给显示出来了,但是呢,并没有做到让它显示到具体的位置,而跟这个位置相关的则需要靠图元装配. 图元装 ...
- 体感设备:因特尔 Intel RealSense R200,乐视LeTV Pro Xtion和Orb奥比中光bec Astra比较
最近调试三个个厂家的体感设备,第一个是Intel的RealSense R200(参数规格:分辨率:1080p,深度有效距离:0.51-4,USB3.0),第二个是乐视LeTV Pro Xtion(参数 ...