[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 ...
随机推荐
- 在 Pandas 中更改列的数据类型
import pandas as pd import numpy as np a = [['a', '1.2', '4.2'], ['b', '70', '0.03'], ['x', '5', '0' ...
- CSUOJ 1009 抛硬币
Description James得到了一堆有趣的硬币,于是决定用这些硬币跟朋友们玩个小游戏.在一个N行M列的表格上,每一个第i行第j列的格子上都放有一枚James的硬币,抛该硬币正面朝上的概率为Pi ...
- Python 编程:从入门到实战 读书笔记
1..title() 每个单词首字母大写 .upper() 所有字母大写 .lower() 所有字母小写 2. \t 空白 转义字符只能用在""内 ...
- Python 中的函数
学了 Python 中的数据类型,语句,接下来就来说一下 Python 中的函数,函数是结构化编程的核心.我们使用函数可以增加程序的可读性.自定义函数时使用关键字def 函数由多条语句组成.在定义函数 ...
- 【转】让你10分钟搞定Mac--最简单快速的虚拟安装
文章出处:让你10分钟搞定Mac--最简单快速的虚拟安装http://bbs.itheima.com/thread-106643-1-1.html (出处: 黑马程序员训练营论坛) 首先说明一下. 第 ...
- Replication Controller
RC保证在同一时间能够运行指定数量的Pod副本,保证Pod总是可用.如果实际Pod数量比指定的多就结束掉多余的,如果实际数量比指定的少就启动缺少的. 当Pod失败.被删除或被终结时,RC会自动创建新的 ...
- 【HDU 3662】3D Convex Hull
http://acm.hdu.edu.cn/showproblem.php?pid=3662 求给定空间中的点的三维凸包上有多少个面. 用增量法,不断加入点,把新加的点能看到的面都删掉,不能看到的面与 ...
- SnackDown Longest Increasing Subsequences 构造题
Longest Increasing Subsequences 题目连接: https://www.codechef.com/SNCKPA16/problems/MAKELIS Description ...
- service redis does not support chkconfig 的解决办法
问题解决办法如下: 必须把下面两行注释放在/etc/init.d/redis文件靠前的注释中(加入以下注释): # chkconfig: # description: Redis is a persi ...
- js跨域请求提示函数未定义的问题
我的代码是这么写的 window.onload=function(){ function sendRequest(){ var script=document.getElementById(" ...