【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 ...
随机推荐
- ubuntu-64位安装tensorflow
(一)ubuntu-64位安装tensorflow过程: 1安装Anaconda 然后执行:bash Anaconda×××-Linux-x86_64.sh ,然后一直enter键,中途会遇到([y] ...
- WIFI万能钥匙协议分析
WIFI万能钥匙协议分析 需求: 上android 市场下载任意一款,wifi万能钥匙 软件,对其进行 协议分析和逆向,达成如下结果:通过对软件的分析,完成自动化爬虫,爬wifi万能钥匙的wifi库, ...
- 在idea中部署远程Tomcat
实现效果:在idea中点击run时,自动将代码编译并上传.部署到远程服务器中 和传统的在本地服务器相比较的优势:1.节省开发者开发机的资源,省去了本地服务器的CPU.内存的占用.2.如果开发的程序为A ...
- JS和CS互访【后台前台代码调用JavaScript变量以及JavaScript调用代码变量】
原文发布时间为:2008-10-13 -- 来源于本人的百度文章 [由搬家工具导入] .如何在JavaScript访问C#函数? 2.如何在JavaScript访问C#变量? 3.如何在C#中访问Ja ...
- py2exe使用方法 (含一些调试技巧,如压缩email 类)
http://justcoding.iteye.com/blog/900993 一.简介 py2exe是一个将python脚本转换成windows上的可独立执行的可执行程序(*.exe)的工具,这样, ...
- Codeforces 404E: Maze 1D(二分)
题意:指令“R”机器人会向右走一步,“L”是向左.起初机器人在0位置,可以在除了0以外的任何位置放障碍,如果机器人的指令将使它走到障碍上,那这一步他会保持不动.要求让机器人最终结束的那一步一定只走过一 ...
- js库-AngularJS
我是一个不思进取的前端. 我想按部就班的工作. 我想得过且过. 老天呀!你咋又逼迫我学习对于我来说的新知识呢!!!!!!!我想哭呀!!!!! 在某个代码项目中我看到了{{??}}这么个标记!我的神呀! ...
- 理解webpack中的devTool的配置项
2.1. eval eval 会将每一个module模块,执行eval,执行后不会生成sourcemap文件,仅仅是在每一个模块后,增加sourceURL来关联模块处理前后对应的关系.在webpac ...
- js 克隆数据 (数组的深浅拷贝)
var a1 = [1,2,3]; var a2 = a1; a2[0] = 90; console.log(a1[0]) //90 解析:数组是复合的数据类型,直接复制的话,只是复制了指向底层数据结 ...
- Careercup | Chapter 4
二叉查换树,左孩子小于等于根,右孩子大于根. 完全二叉树,除最后一层外,每一层上的节点数均达到最大值:在最后一层上只缺少右边的若干结点. complete binary tree 满二叉树,完美二叉树 ...