【LeetCode with Python】 Sort List
2024-09-07 08:42:48
博客域名: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)
最新文章
- 蓝牙Bluetooth技术小知识
- CF449C Jzzhu and Apples (筛素数 数论?
- Javascript 事件(一)
- JQuery好用的日期选择控件 DatePicker
- appendChild()插入节点需注意的问题
- linux中fork()函数详解(转)
- (转)Make命令简介与使用
- bzoj 3672: [Noi2014]购票 树链剖分+维护凸包
- ACM/ICPC ZOJ1003-Crashing Balloon 解题代码
- nodejs原生模块简介
- 基数排序---Java实现+C++实现
- VMI
- Python C++扩展
- css盒子模型(3)
- Python档案袋(文件系列操作 )
- 互斥量mutex的简单使用
- 【代码笔记】iOS-NSFileManager
- Android应用程序进程启动过程(前篇)
- 2017年11月8日最新仿互站导航t5友价商城-9套模板首页都增加微信登陆
- 清除li内a标签的float=left实现a标签在li内居中显示(ul内li不居中显示)
热门文章
- POJ——1195Mobile phones(二维树状数组点修改矩阵查询)
- inux读取ISO文件或是光驱的方法--挂载
- ibatis 字段类型为int时如何避免默认值得干扰
- jquery 日期插件
- 详解DNS域名解析全过程
- vue 权限控制按钮3种样式、内容、以及跳转事件
- const T、const T*、T *const、const T&;、const T*&; 的区别
- android权限大全转http://www.cnblogs.com/classic/archive/2011/06/20/2085055.html
- pandas常见函数详细使用
- 20. Spring Boot Servlet【从零开始学Spring Boot】