博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【leetcode】148. Sort List
阅读量:6227 次
发布时间:2019-06-21

本文共 2941 字,大约阅读时间需要 9 分钟。

题目如下:

Sort a linked list in O(n log n) time using constant space complexity.

Example 1:

Input: 4->2->1->3Output: 1->2->3->4

Example 2:

Input: -1->5->3->4->0Output: -1->0->3->4->5

解题思路:因为题目要求时间复杂度是O(nlogn),所以快排应该是合适的方法。原理也和快速排序差不多,我的方法是引入五个指针,分别是small_head,small_tail,large_head,large_tail,equal_tail,首先对list进行遍历,以head.val作为基准,小于head.val的保存到small的list里面,用small_head,small_tail分别指向small list的头结点和尾节点;大于head.val的保存到large list里面,用large_head,large_tail分别指向 list的头结点和尾节点;等于head.val的存入equal list,复用现有的head结点,equal_tail指向equal的尾节点。分组完成后,再对small list和large list做递归,最后把small list的尾节点指向equal list的head,同时把equal list的尾节点指向large list的头结点即可。

代码如下:

# Definition for singly-linked list.# class ListNode(object):#     def __init__(self, x):#         self.val = x#         self.next = Noneclass Solution(object):    def quick(self,sublist_head):        if sublist_head == None:            return (None,None)        v = sublist_head.val        previous = sublist_head        current = sublist_head.next        little_head = None        little_current = None        large_head = None        large_current = None        equal_current = sublist_head        while current != None:            if current.val < v:                if little_head == None:                    little_current = current                    little_head = current                    current = current.next                    previous.next = current                    little_current.next = None                else:                    little_current.next = current                    little_current = little_current.next                    current = current.next                    previous.next = current                    little_current.next = None            elif current.val == v:                equal_current.next = current                previous = current                equal_current = equal_current.next                current = current.next            else:                if large_head == None:                    large_head = current                    large_current = current                    current = current.next                    previous.next = current                    large_current.next = None                else:                    large_current.next = current                    large_current = large_current.next                    current = current.next                    previous.next = current                    large_current.next = None        small_head,small_tail  =  self.quick(little_head)        big_head,big_tail  =  self.quick(large_head)        if small_head != None:            small_tail.next = sublist_head        equal_current.next = big_head        return (small_head if small_head != None else sublist_head,big_tail if big_tail != None else equal_current)    def sortList(self, head):        """        :type head: ListNode        :rtype: ListNode        """        return self.quick(head)[0]

 

转载于:https://www.cnblogs.com/seyjs/p/10552852.html

你可能感兴趣的文章
Random Processes
查看>>
操作argc, argv的经典写法
查看>>
phpStudy中升级MySQL版本到5.7.17的方法步骤
查看>>
SQLServer BI 学习笔记
查看>>
sublim课程2 sublim编辑器的使用(敲代码的时候把这个放旁边用)
查看>>
什么是Solr
查看>>
oracle 12cR1&12cR2核心高实用性新特性
查看>>
pandas Series的sort_values()方法
查看>>
SQL SERVER CHAR ( integer_expression )各版本返回值差异的案例
查看>>
pytest文档7-pytest-html生成html报告
查看>>
微信小程序弹窗组件
查看>>
安装使用ionic3
查看>>
结构体初始化
查看>>
java中this的N种使用方法
查看>>
Windows IIS安装php
查看>>
mingw 设置python 设置git环境变量
查看>>
linux 系统下如何进行用户之间的切换
查看>>
设计一个算法移除字符串中的重复字符,并写出测试用例。
查看>>
goole机器学习视频链接【学习笔记】
查看>>
查看django版本的方法
查看>>