Sort a linked list in O(n log n) time using constant space complexity.
原题链接: http://oj.leetcode.com/problems/sort-list/
这道题跟Insertion
Sort List类似,要求我们用O(nlogn)算法对链表进行排序,但是并没有要求用哪一种排序算法,我们可以使用归并排序,快速排序,堆排序等满足要求的方法来实现。对于这道题比较容易想到的是归并排序,因为我们已经做过Merge
Two Sorted Lists,这是归并排序的一个subroutine。剩下我们需要做的就是每次找到中点,然后对于左右进行递归,最后用Merge
Two Sorted Lists把他们合并起来。代码如下:
- public ListNode sortList(ListNode head) {
- return mergeSort(head);
- }
- private ListNode mergeSort(ListNode head)
- {
- if(head == null || head.next == null)
- return head;
- ListNode walker = head;
- ListNode runner = head;
- while(runner.next!=null && runner.next.next!=null)
- {
- walker = walker.next;
- runner = runner.next.next;
- }
- ListNode head2 = walker.next;
- walker.next = null;
- ListNode head1 = head;
- head1 = mergeSort(head1);
- head2 = mergeSort(head2);
- return merge(head1, head2);
- }
- private ListNode merge(ListNode head1, ListNode head2)
- {
- ListNode helper = new ListNode(0);
- helper.next = head1;
- ListNode pre = helper;
- while(head1!=null && head2!=null)
- {
- if(head1.val<head2.val)
- {
- head1 = head1.next;
- }
- else
- {
- ListNode next = head2.next;
- head2.next = pre.next;
- pre.next = head2;
- head2 = next;
- }
- pre = pre.next;
- }
- if(head2!=null)
- {
- pre.next = head2;
- }
- return helper.next;
- }
不过用归并排序有个问题就是这里如果把栈空间算上的话还是需要O(logn)的空间的。对于其他排序算法,用兴趣的同学可以实现一下哈。
排序是面试中比较基础的一个主题,所以对于各种常见的排序算法大家还是要熟悉,不了解的朋友可以参见排序算法
- Wiki。特别是算法的原理,很多题目虽然没有直接考察排序的实现,但是用到了其中的思想,比如非常经典的topK问题,就用到了快速排序的原理,关于这个问题在Median
of Two Sorted Arrays中有提到,有兴趣的朋友可以看看。
//https://oj.leetcode.com/problems/sort-list/
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *sortList(ListNode *head) {
if (head == NULL || head->next == NULL) return head;
ListNode *mid = findMid(head);
ListNode *right = sortList(mid->next);
mid->next = NULL;
ListNode *left = sortList(head);
return mergeTwoList(left, right);
}
ListNode *findMid(ListNode *head) {
assert(head != NULL && head->next != NULL);
ListNode *slow = head, *fast = head->next;
while (fast != NULL && fast->next != NULL) {
slow = slow->next;
fast = fast->next->next;
}
return slow;
}
ListNode *mergeTwoList(ListNode *left, ListNode *right) {
ListNode *dummy = new ListNode(-1);
ListNode *tail = dummy;
while (left != NULL && right != NULL) {
if (left->val < right->val) {
tail->next = left;
left = left->next;
}
else {
tail->next = right;
right = right->next;
}
tail = tail->next;
}
if (left != NULL) tail->next = left;
if (right != NULL) tail->next = right;
ListNode *head = dummy->next;
delete dummy;
return head;
}
};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
ListNode *mergeSort(ListNode *head){
if(NULL == head || NULL == head->next) return head;
ListNode *slow = head;
ListNode *fast = head;
while(fast->next != NULL && fast->next->next != NULL){
slow = slow->next;
fast = fast->next->next;
}
ListNode * head2 = slow->next;
slow->next = NULL;
ListNode *head1 = head;
mergeSort(head1);
mergeSort(head2);
return merge(head1,head2);
}
ListNode *merge(ListNode *left,ListNode *right){
ListNode *dummy = new ListNode(0);
ListNode *tail = dummy;
while(NULL != left && NULL != right){
if(left->val < right->val){
tail->next = left;
left = left->next;
}
else{
tail->next = right;
right = right->next;
}
tail = tail->next;
}
if(left != NULL) tail->next = left;
if(NULL != right) tail->next = right;
ListNode *head = dummy->next;
delete dummy;
return head;
}
public:
ListNode *sortList(ListNode *head) {
return mergeSort(head);
}
};
浙公网安备 33010602011771号