Leetcode-Sort List

Description

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

Explain

看題目要求,第一個(gè)是鏈表,第二個(gè)是時(shí)間復(fù)雜度為O(n log n),空間復(fù)雜度為O (1)。排序算法中說到這個(gè)時(shí)間復(fù)雜度的話,肯定也就會(huì)想到快排和歸并排序。歸并排序如果用數(shù)組實(shí)現(xiàn)的話,是做不到空間復(fù)雜度O(1)的,但是這剛好是用鏈表來實(shí)現(xiàn)的,所以用歸并排序O(1)可以達(dá)到要求

Code

/**
 * 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) return head;
        if (!head->next) return head;
        return parti(head);
    }
    
    ListNode* parti(ListNode* cur) {
        if (!cur) return NULL;
        if (!cur->next) return cur;
        int len = 0;
        ListNode* temp = cur;
        while(temp) {
            temp = temp->next;
            len++;
        }
        
        int mid = len / 2;
        int count = 1;
        temp = cur;
        while(count < mid) {
            temp = temp->next;
            count++;
        }
        
        ListNode* next = temp->next;
        temp->next = NULL;
        
        ListNode* one = parti(cur);
        temp = NULL;
        next = parti(next);
        
        while(one || next) {
            if (one && next) {
                
                if (one->val == next->val) {
                    if (!temp) {
                        temp = one;
                        cur = temp;
                    } else {
                        temp->next = one;
                    }
                    ListNode* tmp = one->next;
                    one->next = next;
                    one = tmp;
                    temp = next;
                    next = next->next;
                } else if (one->val > next->val) {
                    if (!temp) {
                        temp = next;
                        cur = temp;
                    } else {
                        temp->next = next;
                        temp = next;
                    }
                    next = next->next;
                } else {
                    if (!temp) {
                        temp = one;
                        cur = temp;
                    } else {
                        temp->next = one;
                        temp = one;
                    }
                    one = one->next;                    
                }
            } else if(one) {
                if (!temp) {
                    cur = one;
                } else {
                    temp->next = one;
                }
                break;
            } else if(next) {
                if (!temp) {
                    cur = next;
                } else {
                    temp->next = next;
                }
                break;                
            } else {
                cur = NULL;
            }
        }
        
        return cur;
    }
};

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • My code: My test result: 這次作業(yè)說實(shí)話不難,但是要完全寫出來還是有點(diǎn)細(xì)節(jié)的。首先,一開始...
    Richardo92閱讀 545評(píng)論 1 1
  • 題目:Sort a linked list in O(nlogn) time using constant spa...
    這是朕的江山閱讀 161評(píng)論 0 0
  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問題, 分享了一些自己做題目的經(jīng)驗(yàn)。 張土汪:刷leetcod...
    土汪閱讀 12,890評(píng)論 0 33
  • 一場(chǎng)秋霜 一抹憂傷 作者:張馨之 筆名:馨之 秋風(fēng)總是那么冰涼,冰涼是因?yàn)榉N下了一場(chǎng)秋霜,這場(chǎng)秋霜的冰涼會(huì)...
    馨之隨筆閱讀 583評(píng)論 0 0
  • 有的時(shí)候,欲望像花朵一樣美麗,引誘著你一步步走向深淵。
    眠花城閱讀 417評(píng)論 4 9

友情鏈接更多精彩內(nèi)容