题目

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. uvm_dpi——DPI在UVM中的实现(一)

    文件: src/dpi/uvm_dpi.svh 类:  无   SystemVerilog DPI,全称SystemVerilog直接编程接口 (英语:SystemVerilog Direct Pro ...

  2. 在 Visual Studio 或 SQLServer Management Studio 的代码编辑器中使用正则表达式匹配日期格式

    使用正则查找时间格式文本 VS正则: (:z表示数字) ':z-:z-:z :z[\:]:z[\:]:z'

  3. openstack RuntimeError: Unable to create a new session key. It is likely that the cache

    [Mon Apr 15 01:02:31.654247 2019] [:error] [pid 19433:tid 139790082479872] Login successful for user ...

  4. 【洛谷】CYJian的水题大赛 解题报告

    点此进入比赛 \(T1\):八百标兵奔北坡 这应该是一道较水的送分题吧. 理论上来说,正解应该是DP.但是,.前缀和优化暴力就能过. 放上我比赛时打的暴力代码吧(\(hl666\)大佬说这种做法的均摊 ...

  5. 从用户访问网站流程开始,细说web网络基础

    1.用户访问网站流程框架 2.dns解析原理 3.tcp/ip三次握手过程原理,11种连接状态 4.tcp/ip四次挥手过程原理,11种连接状态 5.http协议原理(www服务的请求过程)请求细节, ...

  6. 关于java的自增问题

    程序执行结果是0,,,因为count永远是0

  7. 2017年9月11日 梁勇 java教材 编程练习题 第二章 2.15 键盘 读取两个点的坐标值(小数),控制台输出两点间距离。

    package com.swift; import java.util.Scanner; public class PToP { public static void main(String[] ar ...

  8. jenkins重置build序号

    来源:https://www.jianshu.com/p/e342b52d45e1 执行命令:item = Jenkins.instance.getItemByFullName("your- ...

  9. Linux下 tomcat 的开机自启动设置

    每次开机都要启动tomcat,非常麻烦:通过直接修改系统文件,实现tomcat自启动: 1. 修改脚本文件rc.local:vim /etc/rc.d/rc.local 这个脚本是使用者自定的开机启动 ...

  10. 【原创】数据处理中判断空值的方法(np.isnan、is np.nan和pd.isna)比较

      转载请注明出处:https://www.cnblogs.com/oceanicstar/p/10869725.html  1.np.isnan(只有数组数值运算时可使用) 注意:numpy模块的i ...