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)O(NlogN)O(NlogN)。
思路
链表不好操作快排和堆排,使用归并排序(分治法):每次利用快慢指针找到链表的中间位置,将其断开为左右两个子链表,待这两个子链表排序后,利用归并将它们再重新合并为一个有序链表。递归处理。
代码实现
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;
}
}
本文介绍了一种在链表上实现归并排序的方法,通过快慢指针找到中点,将链表分为两部分,递归排序后再合并,达到O(nlogn)的时间复杂度,且仅使用常数空间。
1395

被折叠的 条评论
为什么被折叠?



