0148. Sort List (M)
Sort List (M)
题目
Sort a linked list in O(n log n) time using constant space complexity.
Example 1:
Input: 4->2->1->3
Output: 1->2->3->4
Example 2:
Input: -1->5->3->4->0
Output: -1->0->3->4->5
题意
将链表排序,要求不能使用额外空间,且时间复杂度为\(O(NlogN)\)。
思路
链表不好操作快排和堆排,使用归并排序(分治法):每次利用快慢指针找到链表的中间位置,将其断开为左右两个子链表,待这两个子链表排序后,利用归并将它们再重新合并为一个有序链表。递归处理。
代码实现
Java
class Solution {
public ListNode sortList(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode slow = head, fast = head;
while (fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
}
ListNode mid = slow.next;
slow.next = null;
return merge(sortList(head), sortList(mid));
}
private ListNode merge(ListNode head1, ListNode head2) {
ListNode head = new ListNode(0);
ListNode cur = head;
while (head1 != null && head2 != null) {
if (head1.val < head2.val) {
cur.next = head1;
head1 = head1.next;
} else {
cur.next = head2;
head2 = head2.next;
}
cur = cur.next;
}
cur.next = head1 == null ? head2 : head1;
return head.next;
}
}
JavaScript
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var sortList = function (head) {
if (!head || !head.next) {
return head
}
let slow = head, fast = head
while (fast.next && fast.next.next) {
fast = fast.next.next
slow = slow.next
}
let left = head, right = slow.next
slow.next = null
left = sortList(left)
right = sortList(right)
let dummy = new ListNode()
let p = dummy
while (left && right) {
let node = left.val <= right.val ? left : right
let tmp = node.next
node.next = null
p.next = node
p = p.next
node === left ? (left = tmp) : (right = tmp)
}
p.next = !left ? right : left
return dummy.next
}