150 - 148 排序链表

题目

在 O(n log n) 时间复杂度和常数级空间复杂度下,对链表进行排序。

示例 1:

输入: 4->2->1->3 输出: 1->2->3->4

示例 2:

输入: -1->5->3->4->0 输出: -1->0->3->4->5

解答

归并排序

https://leetcode-cn.com/problems/sort-list/solution/sort-list-gui-bing-pai-xu-lian-biao-by-jyd/

先用快慢指针,把链表从中间拆开,递归排序。排序方法是新建链表。

Picture2.png

Runtime: 224 ms, faster than 73.64% of Python3 online submissions for Sort List.

Memory Usage: 19.6 MB, less than 100.00% of Python3 online submissions for Sort List.

Runtime: 104 ms, faster than 40.33% of JavaScript online submissions for Sort List.

Memory Usage: 40.3 MB, less than 100.00% of JavaScript online submissions for Sort List.

Runtime: 12 ms, faster than 83.94% of Go online submissions for Sort List.

Memory Usage: 5 MB, less than 100.00% of Go online submissions for Sort List.

Last updated

Was this helpful?