原题地址: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的更多相关文章

  1. [leetcode]Sort Colors @ Python

    原题地址:https://oj.leetcode.com/problems/sort-colors/ 题意: Given an array with n objects colored red, wh ...

  2. LeetCode—-Sort List

    LeetCode--Sort List Question Sort a linked list in O(n log n) time using constant space complexity. ...

  3. [LeetCode]题解(python):147-Insertion Sort List

    题目来源: https://leetcode.com/problems/insertion-sort-list/ 题意分析: 用插入排序排序一个链表. 题目思路: 这题没什么好说的,直接用插入排序就行 ...

  4. [leetcode]Insertion Sort List @ Python

    原题地址:http://oj.leetcode.com/problems/insertion-sort-list/ 题意:对链表进行插入排序. 解题思路:首先来对插入排序有一个直观的认识,来自维基百科 ...

  5. [LeetCode]题解(python):090 Subsets II

    题目来源 https://leetcode.com/problems/subsets-ii/ Given a collection of integers that might contain dup ...

  6. [LeetCode]题解(python):078 Subsets

    题目来源 https://leetcode.com/problems/subsets/ Given a set of distinct integers, nums, return all possi ...

  7. [LeetCode]题解(python):049-Groups Anagrams

    题目来源 https://leetcode.com/problems/anagrams/ Given an array of strings, group anagrams together. For ...

  8. [LeetCode]题解(python):047-Permutations II

    题目来源 https://leetcode.com/problems/permutations-ii/ Given a collection of numbers that might contain ...

  9. [LeetCode]题解(python):057-Insert Interval

    题目来源 https://leetcode.com/problems/insert-interval/ Given a set of non-overlapping intervals, insert ...

随机推荐

  1. FPGA+ARM or FPGA+DSP?

    网上有人说.现在的FPGA,ARM功能已经强大到无需DSP协助处理了,未来DSP会不会消声灭迹?是DSP取代FPGA和ARM,还是ARM,FPGA取代DSP呢?担心好不容易学精了DSP,结果DSP变成 ...

  2. MySQL 关于索引那点事

    索引 其实数据库中的数据是按页存放的其实索引也是按页存放的所以本质上索引也占硬盘空间(以最小的消耗,换取最大的利益) 索引是一种有效组合数据的方式!为快速查找到指定记录做铺垫 目的就是快速或者某个记录 ...

  3. Python处理海量数据的实战研究

    最近看了July的一些关于Java处理海量数据的问题研究,深有感触,链接:http://blog.csdn.net/v_july_v/article/details/6685962 感谢July ^_ ...

  4. new Date 兼容性问题

    IOS 11.1.2 iphoneX 下面解析 // 下面的代码会报错 var d = new Date('2018-09-20 19:20:32'); alert(d.getTime());   必 ...

  5. SDOI 2017 Round2 滚粗了

    没进省队qwq 技不如人,甘拜下风

  6. PHP 笔记——文件引用

    1. 文件路径 "文件路径"指的是被包含文件所在的绝对路径或相对路径. 在相对路径中,"./"表示当前目录,"../"表示当前目录的上级目录 ...

  7. 某谷 P5153 简单的函数

    题面在这里 个人感觉这个题可以被打表随便艹过,当然我不是这么做的... 虽然n可达10^18,但随便分析一下就可以发现f(n)是极小的,因为f(n)一步就可以跳到f(前100),不信你算一下前100个 ...

  8. 鸟哥的私房菜:Linux磁盘与文件系统原理

    1 硬盘物理组成     //原理 磁头负责读写     磁道(硬盘同半径的一圈) 磁柱(所有盘磁道叠加起来的柱)     扇区(2条半径将磁道分开的一个扇形区域,是磁盘的最小存储单位) ------ ...

  9. sgu 261

    学习了元根的一些知识,哈哈. 总结一下: 几个概念: 阶:对于模数m和整数a,并且gcd(m,a)==1,那么定义a在模m下的阶r为满足ar=1 mod m的最小正整数. 性质1:r in [1,ph ...

  10. SQL Server 无日志文件附加数据库

    CREATE DATABASE DBname ON (FILENAME = 'D:\SalesData\DBname_data.mdf') FOR ATTACH_REBUILD_LOG ; GO 简单 ...