题目

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 实现的更多相关文章

  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 @ Python

    原题地址:http://oj.leetcode.com/problems/sort-list/ 题意:链表的排序.要求:时间复杂度O(nlogn),空间复杂度O(1). 解题思路:由于题目对时间复杂度 ...

  3. LeetCode—-Sort List

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

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

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

  5. [leetcode]Insertion Sort List @ Python

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. 一些C/C++中的函数

    项目中使用到的C/C++中的一些函数,记录下来加以理解和掌握. 1.memset( ) memset是计算机中C/C++语言函数.将s所指向的某一块内存中的前n个 字节的内容全部设置为ch指定的ASC ...

  2. Linux下安装maven和nexus

    Linux下安装maven1.首先到Maven官网下载安装文件,目前最新版本为3.0.3,下载文件为apache-maven-3.0.3-bin.tar.gz,下载可以使用wget命令: 2.进入下载 ...

  3. jQuery-添加新元素的方法(append()、prepend()、before()、after())

    1.以 HTML 创建新元素 var txt1="<p>Text.</p>"; 2.以 jQuery 创建新元素 var txt2=$("< ...

  4. cms-数据库设计

    业务相关的3张表 1.类型表: CREATE TABLE `t_arctype` (`id` int(11) NOT NULL AUTO_INCREMENT,//id`typeName` varcha ...

  5. ehcache常用API整理

    鉴于csdn的blog的不稳定, 及混乱的编辑器, 和无上传功能, 遂决定彻底投诚javaeye的blog. 数月前整理的一个东西, 作为cache的扫盲文档.参考了它的官方文档. 对ehcache感 ...

  6. 【洛谷3759】[TJOI2017] 不勤劳的图书管理员(树套树)

    点此看题面 大致题意: 给定一个序列,每个元素有两个属性\(a_i\)和\(v_i\),每次操作改变两个元素的位置,求每次操作后\(\sum{v_i+v_j}[i<j,a_i>a_j]\) ...

  7. 关于C#的垃圾回收机制,Finalize和Dispose的区别(自认为很清晰了,有疑问的评论)

    来到个新地方,新学习C#,前面看到C#的垃圾回收,Finalize和Dispose时,总是一知半解,迷迷糊糊.这次好了,前面连续两次面试问到这个问题,脑子里不是很清晰,加上用英文来表达,更是雪上加霜的 ...

  8. python_10_for guess

    age_of_oldboy=56 count=0 for count in range(3): guess_age=int(input('guess age:')) if guess_age==age ...

  9. TensorFlow 内置重要函数解析

    概要 本部分介绍一些在 TensorFlow 中内置的重要函数,了解这些函数有时候更加方便我们进行数据的处理或者构建神经网络. 这些函数如下:       tf.one_hot()     tf.ra ...

  10. 由一道CTF pwn题深入理解libc2.26中的tcache机制

    本文首发安全客:https://www.anquanke.com/post/id/104760 在刚结束的HITB-XCTF有一道pwn题gundam使用了2.26版本的libc.因为2.26版本中加 ...