【LeetCode with Python】 Sort List
博客域名:http://www.xnerv.wang
原题页面:https://oj.leetcode.com/problems/sort-list/
题目类型:
难度评价:★
本文地址:http://blog.csdn.net/nerv3x3/article/details/38143633
Sort a linked list in O(n log n) time using constant space complexity.
class Solution: def findMiddle(self, head):
slow = head
fast = head.next
while None != fast and None != fast.next:
fast = fast.next.next
slow = slow.next
return slow def mergeList(self, head):
if None == head.next:
return head
mid = self.findMiddle(head)
right_head = mid.next
mid.next = None
left_list = self.mergeList(head)
right_list = self.mergeList(right_head)
new_head = ListNode(-1)
new_tail = new_head
left_node = left_list
right_node = right_list
while None != left_node and None != right_node:
if left_node.val <= right_node.val:
new_tail.next = left_node
left_node = left_node.next
else:
new_tail.next = right_node
right_node = right_node.next
new_tail = new_tail.next
new_tail.next = left_node if None != left_node else right_node
return new_head.next # @param head, a ListNode
# @return a ListNode
def sortList(self, head):
if None == head: # leetcode will input this !
return None
return self.mergeList(head)
【LeetCode with Python】 Sort List的更多相关文章
- 【LeetCode with Python】 Rotate Image
博客域名:http://www.xnerv.wang 原标题页:https://oj.leetcode.com/problems/rotate-image/ 题目类型:下标计算 难度评价:★★★ 本文 ...
- 【leetcode 字符串处理】Compare Version Numbers
[leetcode 字符串处理]Compare Version Numbers @author:wepon @blog:http://blog.csdn.net/u012162613 1.题目 Com ...
- 【LeetCode算法-27】Remove Element
LeetCode第27题 Given an array nums and a value val, remove all instances of that value in-place and re ...
- 【head first python】2.共享你的代码 函数模块
#coding:utf-8 #注释代码! #添加两个注释,一个描述模块,一个描述函数 '''这是nester.py模块,提供了一个名为print_lol()的函数, 这个函数的作用是打印列表,其中可能 ...
- 【LeetCode算法-9】Palindrome Number
LeetCode第9题 Determine whether an integer is a palindrome. An integer is a palindrome when it reads t ...
- 【DataStructure In Python】Python实现各种排序算法
使用Python实现直接插入排序.希尔排序.简单选择排序.冒泡排序.快速排序.归并排序.基数排序. #! /usr/bin/env python # DataStructure Sort # Inse ...
- 【Python】 sort、sorted高级排序技巧
文章转载自:脚本之家 这篇文章主要介绍了python sort.sorted高级排序技巧,本文讲解了基础排序.升序和降序.排序的稳定性和复杂排序.cmp函数排序法等内容,需要的朋友可以参考下 Pyth ...
- 【LeetCode】【定制版排序】Sort Colors
之前转载过一篇STL的sort方法底层详解的博客:https://www.cnblogs.com/ygh1229/articles/9806398.html 但是我们在开发中会根据自己特定的应用,有新 ...
- 【leetcode 桶排序】Maximum Gap
1.题目 Given an unsorted array, find the maximum difference between the successive elements in its sor ...
随机推荐
- [转] Makefile 基础 (10) —— Makefile 后序
该篇文章为转载,是对原作者系列文章的总汇加上标注. 支持原创,请移步陈浩大神博客:(最原始版本) http://blog.csdn.net/haoel/article/details/2886 我转自 ...
- python 考试补缺
十六进制: >>> a=100 >>> hex(a) '0x64' 八进制: >>> a=100 >>> oct(a) '0o1 ...
- 我要好offer之 链表大总结
单链表是一种递归结构,可以将单链表看作特殊的二叉树(我把它叫做一叉树) 单链表的定义: /** * Definition for singly-linked list. * struct ListNo ...
- scss 侧边栏_图片
html <!doctype html><html lang="zh-CN"><head> <meta charset="UTF ...
- Java面试进阶部分集合
框架基础 反射:反射是Java开发的一类动态相关机制.因为本身Java语言并不是一款动态语言,如果我们想要得到程序动态的效果,因此便引入了反射机制这一概念. 怎么表达反射? 能用反射做什么? PS:某 ...
- FileUtils删除文件的工具类
前提是知道文件在哪个文件夹下面然后到文件夹下面删除文件,如果文件夹也需要传参数需要对下面方法进行改造. ( 需要借助于commons-io.jar和ResourceUtils.java ) 1.De ...
- 转 C++中不能声明为虚函数的有哪些函数
传送门 C++中不能声明为虚函数的有哪些函数 常见的不不能声明为虚函数的有:普通函数(非成员函数):静态成员函数:内联成员函数:构造函数:友元函数. 1.为什么C++不支持普通函数为虚函数? 普通函数 ...
- 遍历删除List中的元素,会报错?
经常会碰到遍历集合,然后删除里面的对象报错, 纠结半天, 百度了一下,有大神说不能用for-each, for , 只能用迭代器,真的吗? 我就删成功了呢,看代码,请大神们指正! public s ...
- js-点击按钮页面滚动到顶部,底部,指定位置
之所以笔记一下这个,因为我在项目中经常用到. $('.scroll_top').click(function(){$('html,body').animate({scrollTop: '0px'}, ...
- bzoj 4921: [Lydsy六月月赛]互质序列
4921: [Lydsy六月月赛]互质序列 Time Limit: 1 Sec Memory Limit: 256 MBSubmit: 188 Solved: 110[Submit][Status ...