【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 ...
随机推荐
- SharePoint 2013 App 开发—App开发概述
基于安全性的考虑,SharePoint App 不能像其它两种方式一样,直接使用安全性更高的服务端代码的API.Javascript 扮演极为重要的角色,在SharePoint App中与ShareP ...
- “百度杯”CTF比赛 十月场_GetFlag(验证码爆破+注入+绝对路径文件下载)
题目在i春秋ctf大本营 页面给出了验证码经过md5加密后前6位的值,依照之前做题的套路,首先肯定是要爆破出验证码,这里直接给我写的爆破代码 #coding:utf-8 import hashlib ...
- 标准C程序设计七---77
Linux应用 编程深入 语言编程 标准C程序设计七---经典C11程序设计 以下内容为阅读: <标准C程序设计>(第7版) 作者 ...
- linux下网络监控神器"iptraf-ng"
优点:监控的网络信息很全面,安装和使用方便 #centos安装: #yum 源使用centos自带的base源即可. yum install -y iptraf-ng #运行 iptraf-n ...
- AC日记——最短路 洛谷 P2384
题目背景 狗哥做烂了最短路,突然机智的考了Bosh一道,没想到把Bosh考住了...你能帮Bosh解决吗? 他会给你100000000000000000000000000000000000%10金币w ...
- 在 .Net Core xUnit test 项目中使用配置文件
在对项目做集成测试的时候,经常会需要用到一些参数比如用户名密码等,这些参数不宜放在测试代码中.本文介绍一种方法:使用配置文件. 添加配置文件 在集成测试项目目录下新建文件:Configuration. ...
- Python通用编程
本文是Python通用编程系列教程,已全部更新完成,实现的目标是从零基础开始到精通Python编程语言.本教程不是对Python的内容进行泛泛而谈,而是精细化,深入化的讲解,共5个阶段,25章内容.所 ...
- netcore3.0 webapi集成Swagger 5.0
在项目中引用Swashbuckle.AspNetCore和Swashbuckle.AspNetCore.Filters两个dll,在Startup中的ConfigureServices相关配置代码如下 ...
- 某考试 T3 bitboard
bitboardDiscription 天才发明家小K 制造了一块比特板.板子上有2^n个比特元,编号为0 ∼ 2^n−1.每个比特元
- Redis - 事务操作与详解
https://blog.csdn.net/J080624/article/details/81669560 写的比较清楚的一个帖子