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

LeetCode OJ - Sort List

时间:2014-05-16 05:49:04      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   c   java   

题目:

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

解题思路:

  复杂度为O(n* logn) 的排序算法有:快速排序、堆排序、归并排序。对于链表这种数据结构,使用归并排序比较靠谱。递归代码如下:

代码:

  

bubuko.com,布布扣
/**
 * 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* p_middle = head;
        ListNode* p_last = head->next;
        while(p_last != NULL && p_last->next != NULL){
            p_middle = p_middle->next;
            p_last = p_last->next->next;
        }
        
        /*划分为左右两部分*/
        ListNode* p_left = head;
        ListNode* p_right = p_middle->next;
        p_middle->next = NULL;
        
        /*对左右两部分分别进行排序*/
        p_left = sortList(p_left);
        p_right = sortList(p_right);
        
        /*合并*/
        ListNode* p_head = NULL;
        if(p_left->val < p_right->val){
            p_head = p_left;
            p_left = p_left->next;
        }else{
            p_head = p_right;
            p_right = p_right->next;
        }
        
        ListNode* p_current = p_head;
        while (p_left != NULL && p_right != NULL) {
            if (p_left->val < p_right->val){
                p_current->next = p_left;
                p_left = p_left->next;
            } else {
                p_current->next = p_right;
                p_right = p_right->next;
            }
            p_current = p_current->next;
        }
        if (p_left != NULL) p_current->next = p_left;
        if (p_right != NULL) p_current->next = p_right;
        
        return p_head;
    }
};
bubuko.com,布布扣

 

LeetCode OJ - Sort List,布布扣,bubuko.com

LeetCode OJ - Sort List

标签:style   blog   class   code   c   java   

原文地址:http://www.cnblogs.com/dongguangqing/p/3726297.html

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