148. Sort List Medium
Given the head of a linked list, return the list after sorting it in ascending order.
Follow up: Can you sort the linked list in O(n logn) time and O(1) memory (i.e. constant space)?
Example 1:

1 | Input: head = [4,2,1,3] |
Example 2:

1 | Input: head = [-1,5,3,4,0] |
Example 3:
1 | Input: head = [] |
Constraints:
- The number of nodes in the list is in the range
[0, 5 * 104]. -105 <= Node.val <= 105
思路:
思路一:递归法
- 归并排序
- 找链表中点
- 哨兵
本题是一个无序链表的排序问题,要求是空间复杂度为 O(nlogn) ,那只能想到快速排序、归并排序、堆排序,快排上通过索引的,不适合用在链表中;堆排序新建结点中;这题可采用归并排序,在 21题 中介绍了合并两个有序列表,现在只是一个无序链表,没有条件创造条件:
- 两个链表:找到链表中点,切断
- 有序:一个无序链表切开后,变成了两个无序链表,再对每一个链表进行递归,当只有一个结点,自然就是有序的了
代码:
1 | public ListNode sortList(ListNode head) { |
时间复杂度:O(nlogn)
空间复杂度:O(1) 其实并不是 1 ,由于递归要调用栈