首页 > 其他 > 详细

LeetCode: Sort List 解题报告

时间:2014-11-29 21:26:49      阅读:301      评论:0      收藏:0      [点我收藏+]

Sort List

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

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

bubuko.com,布布扣

SOLUTION 1:

使用Merge Sort来解决问题。

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

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

是为了解决:

1->2->null

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

bubuko.com,布布扣
 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

GITHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/list/SortList.java

LeetCode: Sort List 解题报告

原文:http://www.cnblogs.com/yuzhangcmu/p/4131885.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!