[leetcode sort]148. Sort List
Sort a linked list in O(n log n) time using constant space complexity.
以时间复杂度O(n log n)排序一个链表。
归并排序,在链表中不需要多余的主存空间
tricks:用快慢指针找到中间位置,划分链表
class Solution(object):
def sortList(self, head):
if not head or not head.next:
return head
pre, slow, fast = None, head, head
while fast and fast.next:
pre, slow, fast = slow, slow.next, fast.next.next
pre.next = None
return self.merge(*(map(self.sortList,(head,slow))))
def merge(self,h1,h2):
dummy = tail = ListNode(None)
while h1 and h2:
if h1.val < h2.val:
tail.next,h1, tail = h1,h1.next,h1
else:
tail.next,h2,tail = h2,h2.next,h2
tail.next = h1 or h2
return dummy.next
[leetcode sort]148. Sort List的更多相关文章
- 【刷题-LeetCode】148 Sort List
Sort List Sort a linked list in O(n log n) time using constant space complexity. Example 1: Input: 4 ...
- Leetcode之148. Sort List Medium
https://leetcode.com/problems/sort-list/ Sort a linked list in O(n log n) time using constant space ...
- 【LeetCode】148. Sort List 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【leetcode】148. Sort List
Sort a linked list in O(n log n) time using constant space complexity. 链表排序可以用很多方法,插入,冒泡,选择都可以,也容易实现 ...
- C#版 - LeetCode 148. Sort List 解题报告(归并排序小结)
leetcode 148. Sort List 提交网址: https://leetcode.com/problems/sort-list/ Total Accepted: 68702 Total ...
- [LeetCode] 148. Sort List 链表排序
Sort a linked list in O(n log n) time using constant space complexity. Example 1: Input: 4->2-> ...
- [LeetCode] 148. Sort List 解题思路
Sort a linked list in O(n log n) time using constant space complexity. 问题:对一个单列表排序,要求时间复杂度为 O(n*logn ...
- 148. Sort List - LeetCode
Solution 148. Sort List Question 题目大意:对链表进行排序 思路:链表转为数组,数组用二分法排序 Java实现: public ListNode sortList(Li ...
- 【LeetCode】排序 sort(共20题)
链接:https://leetcode.com/tag/sort/ [56]Merge Intervals (2019年1月26日,谷歌tag复习) 合并区间 Input: [[1,3],[2,6], ...
随机推荐
- 渐变色之location概念.
CHENYILONG Blog 渐变色之location概念.全屏幕13-12-22 上午10:18 © chenyilong. Powered by Postach.io Blog
- laravel带条件查询手动分页
后台php代码: //手动分页 $users = $kaoqin; //打算输出的数组,二维 $perPage = 10; if ($request->has('page')) { $curre ...
- Python概念-__del__的悲伤
__del__了不得了,这个是在回收实例化对象时触发执行的方法 每当del 实例化对象时会触发 或者是程序结束时,会触发,总之就是实例化对象失效时都会执行__del__方法 代码示例: class F ...
- accept系统调用
/* * For accept, we attempt to create a new socket, set up the link * with the client, wake up the c ...
- 金蝶K3WISE常用数据表
K3Wise 14.2 清空密码update t_User set FSID=') F ", ,P T #8 *P!D &D 80!N &@ <0 C '+''''+' ...
- 关于oracle数据库死锁的检查方法
一.数据库死锁的现象程序在执行的过程中,点击确定或保存按钮,程序没有响应,也没有出现报错. 二.死锁的原理当对于数据库某个表的某一列做更新或删除等操作,执行完毕后该条语句不提交,另一条对于这一列数据做 ...
- plupload 上传组件的使用
在这之前在感谢园子好多大牛的文章,在这里就不列出来了. 进入正题. svn检索https://github.com/moxiecode/plupload 获取到代码,这篇文章使用的是v2.1.8 主要 ...
- sqlserver中查询存储过程中的字符串
select name from sysobjects o, syscomments s where o.id = s.id and text like '%querytext%' and o.xty ...
- python面向对象(五)之多态
继承 在讲多态之前我们再复习下继承,下面是一个例子. Circle 和 Rectangle 继承自 Shape,不同的图形,面积(area)计算方式不同. # shape.py class S ...
- jQuery之字体大小的设置
先获取字体大小,进行处理. 再将修改的值保存. slice() 方法可从已有的数组中返回选定的元素.arrayObject.slice(start,end).start 必需.规定从何处开始选 ...