博客域名: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)

最新文章

  1. 蓝牙Bluetooth技术小知识
  2. CF449C Jzzhu and Apples (筛素数 数论?
  3. Javascript 事件(一)
  4. JQuery好用的日期选择控件 DatePicker
  5. appendChild()插入节点需注意的问题
  6. linux中fork()函数详解(转)
  7. (转)Make命令简介与使用
  8. bzoj 3672: [Noi2014]购票 树链剖分+维护凸包
  9. ACM/ICPC ZOJ1003-Crashing Balloon 解题代码
  10. nodejs原生模块简介
  11. 基数排序---Java实现+C++实现
  12. VMI
  13. Python C++扩展
  14. css盒子模型(3)
  15. Python档案袋(文件系列操作 )
  16. 互斥量mutex的简单使用
  17. 【代码笔记】iOS-NSFileManager
  18. Android应用程序进程启动过程(前篇)
  19. 2017年11月8日最新仿互站导航t5友价商城-9套模板首页都增加微信登陆
  20. 清除li内a标签的float=left实现a标签在li内居中显示(ul内li不居中显示)

热门文章

  1. POJ——1195Mobile phones(二维树状数组点修改矩阵查询)
  2. inux读取ISO文件或是光驱的方法--挂载
  3. ibatis 字段类型为int时如何避免默认值得干扰
  4. jquery 日期插件
  5. 详解DNS域名解析全过程
  6. vue 权限控制按钮3种样式、内容、以及跳转事件
  7. const T、const T*、T *const、const T&amp;、const T*&amp; 的区别
  8. android权限大全转http://www.cnblogs.com/classic/archive/2011/06/20/2085055.html
  9. pandas常见函数详细使用
  10. 20. Spring Boot Servlet【从零开始学Spring Boot】