LeetCode: Sort List [148]

【题目】

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

【题意】

排序一个链表,要求时间复杂度O(nlogn),使用常量空间

【思路】

nlogn的复杂度,用归并排序求解

【代码】

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *getMid(ListNode* head){
        if(head==NULL || head->next==NULL)return head;
        ListNode*prev=NULL;
        ListNode*p1=head;
        ListNode*p2=head;
        while(p2){
            prev=p1;
            p1=p1->next;
            p2=p2->next;
            if(p2)p2=p2->next;
        }
        prev->next=NULL;
        return p1;
    }
    ListNode *sortList(ListNode *head) {
        // nlogn的复杂度,用归并排序求解
        if(head==NULL || head->next==NULL)return head;
        //原列表平分为两个列表
        ListNode*head1=head;
        ListNode*head2=getMid(head);
        //分别对两个列表进行排序
        ListNode*p1=sortList(head1);
        ListNode*p2=sortList(head2);
        ListNode*newhead=NULL;
        ListNode*p=NULL;
        //归并
        while(p1&&p2){
            if(p1->val<p2->val){
                if(p==NULL)newhead=p1;
                else p->next=p1;
                p=p1;
                p1=p1->next;
            }
            else{
                if(p==NULL) newhead=p2;
                else p->next=p2;
                p=p2;
                p2=p2->next;
            }
        }
        if(p1) p->next=p1;
        if(p2) p->next=p2;

        return newhead;
    }
};

LeetCode: Sort List [148]

时间: 2024-08-10 06:39:41

LeetCode: Sort List [148]的相关文章

leetcode Sort Colors

*/--> pre.src {background-color: Black; color: White;} pre.src {background-color: Black; color: White;} pre.src {background-color: Black; color: White;} leetcode Sort Colors 计数排序 注: 题目的要求是将 A 数组重新排列成有序, 而不是将排序的序列输出 Sort Colors Given an array with n o

[leetcode]Sort List @ Python

原题地址:http://oj.leetcode.com/problems/sort-list/ 题意:链表的排序.要求:时间复杂度O(nlogn),空间复杂度O(1). 解题思路:由于题目对时间复杂度和空间复杂度要求比较高,所以查看了各种解法,最好的解法就是归并排序,由于链表在归并操作时并不需要像数组的归并操作那样分配一个临时数组空间,所以这样就是常数空间复杂度了,当然这里不考虑递归所产生的系统调用的栈.   这里涉及到一个链表常用的操作,即快慢指针的技巧.设置slow和fast指针,开始它们都

LeetCode[Sort]: Largest Number

LeetCode[Sort]: Largest Number Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. Note: The result may be very large, so you need to

[leetcode]Sort Colors @ Python

原题地址:https://oj.leetcode.com/problems/sort-colors/ 题意: Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integer

LeetCode: Sort Colors 题解

Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, and bl

LeetCode::Sort List 详细分析

Sort a linked list in O(n log n) time using constant space complexity. 这道题目非常简短的一句话,给链表排序,看到nlogn,我们可以来简单复习一下排序.首先说一下这个nlogn的时间复杂度(根据决策树我们可以得出这个界限),是基于比较排序的最小上限,也就是说,对于没有一定范围情况的数据来说,最快的排序思路就是归并和快速排序了(当然具体的参数系数还是由更具体的设置决定的).对于数组的话,如果使用归并排序,不是in place的

LeetCode: Sort Colors [075]

[题目] Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, a

LeetCode——Sort Colors

Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, and bl

[LeetCode] Sort Colors [23]

题目 Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, and