148.Sort List (M)
https://leetcode.com/problems/sort-list/
1.Description(Medium)
Sort a linked list in O(n_log_n) time using constant space complexity.
Example
Given1->3->2->null
, sort it to1->2->3->null
.
2.Code
排序要求O(nlogn)的时间,就是Quicksort, Mergesort, Heapsort
没有n次new语句的就是constant space.
Merge Sort:
1> 找中点 middle,断开前后两个链表
2> Recursive (Divided left right)
3> Merge (left,right)
Last updated