码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode[148]-Sort List

时间:2015-06-10 14:15:10      阅读:102      评论:0      收藏:0      [点我收藏+]

标签:mergesort   归并排序   单链表排序   合并   sort   

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


分析:题目要求时间复杂度为O(nlogn),所以一开始想到的就是快速排序,但是快速排序一直AC不了,然后就想到用归并排序,没想到归并排序竟然可以。下面给出详细代码:

归并排序需要做的

  • 找到中间点
  • 合并两个排好序的链表
  • 递归实现归并排序

Code(c++):

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    //mergeSort
    ListNode* sortList(ListNode* head) {

        if(head == NULL || head->next==NULL) return head;

        ListNode *mid = getMid(head);
        ListNode *left = head,*right;

        if(mid){
            cout<<mid->val<<endl;
            right = mid->next;
            mid->next = NULL;
        }

        return mergeLinkedList(sortList(left),sortList(right));

    }
    //get middle point from ListNode
    ListNode* getMid(ListNode* head){
        if(head==NULL || head->next==NULL ) return head;

        ListNode* first = head,* second = head->next;

        while(second && second->next){
            first = first->next;
            second = second->next->next;
        }
        return first;
    }

    //merge two sorted Linked List
    ListNode* mergeLinkedList(ListNode* first,ListNode* second){
        if(first==NULL) return second;
        if(second==NULL) return first;

        ListNode* tail,* front;
        front = new ListNode(-1);
        tail = front;
        while(first && second){
            if(first->val < second->val){
                tail->next = first;
                first = first->next;
                tail = tail->next;
            }else{
                tail->next = second;
                second = second->next;
                tail = tail->next;
            }
        }
        if(first){
            tail->next =first;
        }
        if(second){
            tail->next = second;
        }
        front = front->next;
        return front;
    }
};

Leetcode[148]-Sort List

标签:mergesort   归并排序   单链表排序   合并   sort   

原文地址:http://blog.csdn.net/dream_angel_z/article/details/46440767

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!