题目

Sort a linked list in O(n log n) time using constant space complexity.

Example 1:

  1. Input: 4->2->1->3
  2. Output: 1->2->3->4

Example 2:

  1. Input: -1->5->3->4->0
  2. Output: -1->0->3->4->5

题意

将链表排序,要求不能使用额外空间,且时间复杂度为$O(NlogN)$。

思路

链表不好操作快排和堆排,使用归并排序(分治法):每次利用快慢指针找到链表的中间位置,将其断开为左右两个子链表,待这两个子链表排序后,利用归并将它们再重新合并为一个有序链表。递归处理。


代码实现

Java

  1. class Solution {
  2. public ListNode sortList(ListNode head) {
  3. if (head == null || head.next == null) {
  4. return head;
  5. }
  6. ListNode slow = head, fast = head;
  7. while (fast.next != null && fast.next.next != null) {
  8. slow = slow.next;
  9. fast = fast.next.next;
  10. }
  11. ListNode mid = slow.next;
  12. slow.next = null;
  13. return merge(sortList(head), sortList(mid));
  14. }
  15. private ListNode merge(ListNode head1, ListNode head2) {
  16. ListNode head = new ListNode(0);
  17. ListNode cur = head;
  18. while (head1 != null && head2 != null) {
  19. if (head1.val < head2.val) {
  20. cur.next = head1;
  21. head1 = head1.next;
  22. } else {
  23. cur.next = head2;
  24. head2 = head2.next;
  25. }
  26. cur = cur.next;
  27. }
  28. cur.next = head1 == null ? head2 : head1;
  29. return head.next;
  30. }
  31. }

JavaScript

  1. /**
  2. * Definition for singly-linked list.
  3. * function ListNode(val, next) {
  4. * this.val = (val===undefined ? 0 : val)
  5. * this.next = (next===undefined ? null : next)
  6. * }
  7. */
  8. /**
  9. * @param {ListNode} head
  10. * @return {ListNode}
  11. */
  12. var sortList = function (head) {
  13. if (!head || !head.next) {
  14. return head
  15. }
  16. let slow = head, fast = head
  17. while (fast.next && fast.next.next) {
  18. fast = fast.next.next
  19. slow = slow.next
  20. }
  21. let left = head, right = slow.next
  22. slow.next = null
  23. left = sortList(left)
  24. right = sortList(right)
  25. let dummy = new ListNode()
  26. let p = dummy
  27. while (left && right) {
  28. let node = left.val <= right.val ? left : right
  29. let tmp = node.next
  30. node.next = null
  31. p.next = node
  32. p = p.next
  33. node === left ? (left = tmp) : (right = tmp)
  34. }
  35. p.next = !left ? right : left
  36. return dummy.next
  37. }