leetcode 【 Sort List 】 python 实现
题目:
Sort a linked list in O(n log n) time using constant space complexity.
代码:oj 测试通过 Runtime: 372 ms
# 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 sortList(self, head): if head is None or head.next is None:
return head slow = head
fast = head while fast.next is not None and fast.next.next is not None:
slow = slow.next
fast = fast.next.next h1 = head
h2 = slow.next
slow.next = None
l1 = self.sortList(h1)
l2 = self.sortList(h2)
head = self.mergeTwoLists(l1,l2) return head # merger two sorted list
def mergeTwoLists(self, l1, l2):
if l1 is None:
return l2
if l2 is None:
return l1 p = ListNode(0)
dummyhead = p 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 None:
p.next = l2
else:
p.next = l1 return dummyhead.next
思路:
归并排序的原理无需多说,感觉像小学老师给卷子排序:每个小组的组内成绩由低到高排序;小组长再交给大组长;大组长再交给老师。
小白实现的步骤是先测试通过mergeTwoLists函数,实现两个有序list的合并。详情见http://www.cnblogs.com/xbf9xbf/p/4186905.html这篇日志。
在实现两个有序链表的合并基础上,再在主程序写递归程序。
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 @ Python
原题地址:http://oj.leetcode.com/problems/sort-list/ 题意:链表的排序.要求:时间复杂度O(nlogn),空间复杂度O(1). 解题思路:由于题目对时间复杂度 ...
- 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 ...
随机推荐
- Yii2.0 高级版安装 windows
最近在学习yii2.0 在安装高级版的时候遇到一些问题 索性解决了 下面分享一下 一.关于下载 自行百度,在Yii Framework 中文社区 下载专区下载高级应用程序模板(这边下载用电信网络不用下 ...
- Retrofit2与RxJava用法大全
Retrofit2是square公司出品的一个网络请求库,网上有很多相关的介绍.我很久以前都想去研究了,但一直都有各种事情耽搁,现在就让我们一起去捋一捋,这篇主要讲解Retrofit2与RxJava的 ...
- uvm_svcmd_dpi——DPI在UVM中的实现(二)
UVM中有需要从cmmand line 输入参数的需求,所有uvm_svcmd_dpi.svh和uvm_svcmd_dpi.cc 文件就是实现功能. uvm_svcmd_dpi.svh的源代码如下,我 ...
- linux服务器免密钥登录
方法一:通过下载服务器私钥方式 服务器端执行: ssh-keygen -t rsa -b 4096 cat ~/.ssh/id_rsa.pub > ~/.ssh/authorized_keys ...
- Linux uart程序
我用的是jetson tx1 开发板 都是linux系统出了串口文件可能不同其他的没有什么不同都能用. 我安装的是qt5 新建一个none qt c工程,用c 语言开发 期间调试了两天结果还是发送和 ...
- linux 命令——39 grep (转)
Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全局正则表达 ...
- ffmpeg 命令2
ffmpeg常用基本命令(转) [FFmpeg]FFmpeg常用基本命令 1.分离视频音频流 ffmpeg -i input_file -vcodec copy -an output_file_vid ...
- c#树形结构
http://download.csdn.net/detail/rememberme001/6997235 http://bbs.csdn.net/topics/310094150 http://ww ...
- WPF中批量进行验证操作
//ref,out private void CheckTextboxNotEmpty(ref bool isOK, params TextBox[] textboxes) { foreach (Te ...
- 支持无限加载的js图片画廊插件
natural-gallery-js是一款支持无限加载的js图片画廊插件.该js图片画廊支持图片的懒加载,可以对图片进行搜索,分类,还可以以轮播图的方式来展示和切换图片. 使用方法 在页面中引入下面的 ...