Sort List

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

使用Merge Sort, 空间复杂度是 O(logN) 因为使用了栈空间。

LeetCode: Sort List 解题报告

SOLUTION 1:

使用Merge Sort来解决问题。

为什么不用QuickSort?
因为随机访问对于链表而言太耗时,而heap sort不可行。

注意,Find Mid用了2种解法。或者是让Fast提前结束,或是让Fast先走一步,目的就是要取得中间节点的前一个。这样做的目的,主要

是为了解决:

1->2->null

这一种情况。如果不这么做,slow会返回2.这样我们没办法切割2个Node的这种情况。

 1 /**
 2  * Definition for singly-linked list.
 3  * class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode(int x) {
 7  *         val = x;
 8  *         next = null;
 9  *     }
10  * }
11  */
12 public class Solution {
13     public ListNode sortList(ListNode head) {
14         // Nodes should be more than 2.
15         if (head == null || head.next == null) {
16             return head;
17         }
18         
19         // get the mid node.
20         ListNode midPre = getMidPre(head);
21         
22         // Cut the two list.
23         ListNode right = midPre.next;
24         midPre.next = null;
25         
26         // Sort the left side and the right side.
27         ListNode left = sortList(head);
28         right = sortList(right);
29         
30         // Merge the two sides together.
31         return merge(left, right);
32     }
33     
34     // get the pre node before mid.
35     public ListNode getMidPre1(ListNode head) {
36         ListNode slow = head;
37         ListNode fast = head;
38         
39         while (fast != null && fast.next != null && fast.next.next != null) {
40             slow = slow.next;
41             fast = fast.next.next;
42         }
43         
44         return slow;
45     }
46     
47     // get the pre node before mid.
48     public ListNode getMidPre(ListNode head) {
49         ListNode slow = head;
50         
51         // fast提前一点儿。这样就可以得到前一个节点喽。
52         ListNode fast = head.next;
53         
54         while (fast != null && fast.next != null) {
55             slow = slow.next;
56             fast = fast.next.next;
57         }
58         
59         return slow;
60     }
61     
62     public ListNode merge(ListNode head1, ListNode head2) {
63         ListNode dummy = new ListNode(0);
64         ListNode cur = dummy;
65         
66         while (head1 != null && head2 != null) {
67             if (head1.val < head2.val) {
68                 cur.next = head1;
69                 head1 = head1.next;
70             } else {
71                 cur.next = head2;
72                 head2 = head2.next;
73             }
74             
75             cur = cur.next;
76         }
77         
78         if (head1 != null) {
79             cur.next = head1;
80         } else {
81             cur.next = head2;
82         }
83         
84         return dummy.next;
85     }
86 }
View Code

相关文章:

  • 2021-07-13
  • 2022-01-22
  • 2021-10-20
  • 2022-01-21
  • 2022-12-23
  • 2021-09-14
  • 2021-06-07
  • 2022-12-23
猜你喜欢
  • 2021-11-17
  • 2021-12-06
  • 2021-05-18
  • 2021-08-08
  • 2021-10-28
  • 2021-08-18
  • 2021-11-01
相关资源
相似解决方案