欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++實(shí)現(xiàn)LeetCode(148.鏈表排序)

 更新時(shí)間:2021年07月28日 17:03:38   作者:Grandyang  
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(148.鏈表排序),本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下

[LeetCode] 148. Sort List 鏈表排序

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

Example 1:

Input: 4->2->1->3
Output: 1->2->3->4

Example 2:

Input: -1->5->3->4->0
Output: -1->0->3->4->5

常見排序方法有很多,插入排序,選擇排序,堆排序,快速排序,冒泡排序,歸并排序,桶排序等等。。它們的時(shí)間復(fù)雜度不盡相同,而這里題目限定了時(shí)間必須為O(nlgn),符合要求只有快速排序,歸并排序,堆排序,而根據(jù)單鏈表的特點(diǎn),最適于用歸并排序。為啥呢?這是由于鏈表自身的特點(diǎn)決定的,由于不能通過坐標(biāo)來直接訪問元素,所以快排什么的可能不太容易實(shí)現(xiàn)(但是被評(píng)論區(qū)的大神們打臉,還是可以實(shí)現(xiàn)的),堆排序的話,如果讓新建結(jié)點(diǎn)的話,還是可以考慮的,若只能交換結(jié)點(diǎn),最好還是不要用。而歸并排序(又稱混合排序)因其可以利用遞歸來交換數(shù)字,天然適合鏈表這種結(jié)構(gòu)。歸并排序的核心是一個(gè) merge() 函數(shù),其主要是合并兩個(gè)有序鏈表,這個(gè)在 LeetCode 中也有單獨(dú)的題目 Merge Two Sorted Lists。由于兩個(gè)鏈表是要有序的才能比較容易 merge,那么對(duì)于一個(gè)無序的鏈表,如何才能拆分成有序的兩個(gè)鏈表呢?我們從簡(jiǎn)單來想,什么時(shí)候兩個(gè)鏈表一定都是有序的?就是當(dāng)兩個(gè)鏈表各只有一個(gè)結(jié)點(diǎn)的時(shí)候,一定是有序的。而歸并排序的核心其實(shí)是分治法 Divide and Conquer,就是將鏈表從中間斷開,分成兩部分,左右兩邊再分別調(diào)用排序的遞歸函數(shù) sortList(),得到各自有序的鏈表后,再進(jìn)行 merge(),這樣整體就是有序的了。因?yàn)樽渔湵淼倪f歸函數(shù)中還是會(huì)再次拆成兩半,當(dāng)拆到鏈表只有一個(gè)結(jié)點(diǎn)時(shí),無法繼續(xù)拆分了,而這正好滿足了前面所說的“一個(gè)結(jié)點(diǎn)的時(shí)候一定是有序的”,這樣就可以進(jìn)行 merge 了。然后再回溯回去,每次得到的都是有序的鏈表,然后進(jìn)行 merge,直到還原整個(gè)長(zhǎng)度。這里將鏈表從中間斷開的方法,采用的就是快慢指針,大家可能對(duì)快慢指針找鏈表中的環(huán)比較熟悉,其實(shí)找鏈表中的中點(diǎn)同樣好使,因?yàn)榭熘羔樏看巫邇刹?,慢指針每次走一步,?dāng)快指針到達(dá)鏈表末尾時(shí),慢指針正好走到中間位置,參見代碼如下:

C++ 解法一:

class Solution {
public:
    ListNode* sortList(ListNode* head) {
        if (!head || !head->next) return head;
        ListNode *slow = head, *fast = head, *pre = head;
        while (fast && fast->next) {
            pre = slow;
            slow = slow->next;
            fast = fast->next->next;
        }
        pre->next = NULL;
        return merge(sortList(head), sortList(slow));
    }
    ListNode* merge(ListNode* l1, ListNode* l2) {
        ListNode *dummy = new ListNode(-1);
        ListNode *cur = dummy;
        while (l1 && l2) {
            if (l1->val < l2->val) {
                cur->next = l1;
                l1 = l1->next;
            } else {
                cur->next = l2;
                l2 = l2->next;
            }
            cur = cur->next;
        }
        if (l1) cur->next = l1;
        if (l2) cur->next = l2;
        return dummy->next;
    }
};

Java 解法一:

public class Solution {
    public ListNode sortList(ListNode head) {
        if (head == null || head.next == null) return head;
        ListNode slow = head, fast = head, pre = head;
        while (fast != null && fast.next != null) {
            pre = slow;
            slow = slow.next;
            fast = fast.next.next;
        }
        pre.next = null;
        return merge(sortList(head), sortList(slow));
    }
    public ListNode merge(ListNode l1, ListNode l2) {
        ListNode dummy = new ListNode(-1);
        ListNode cur = dummy;
        while (l1 != null && l2 != null) {
            if (l1.val < l2.val) {
                cur.next = l1;
                l1 = l1.next;
            } else {
                cur.next = l2;
                l2 = l2.next;
            }
            cur = cur.next;
        }
        if (l1 != null) cur.next = l1;
        if (l2 != null) cur.next = l2;
        return dummy.next;
    }
}

下面這種方法也是歸并排序,而且在merge函數(shù)中也使用了遞歸,這樣使代碼更加簡(jiǎn)潔啦~

C++ 解法二:

class Solution {
public:
    ListNode* sortList(ListNode* head) {
        if (!head || !head->next) return head;
        ListNode *slow = head, *fast = head, *pre = head;
        while (fast && fast->next) {
            pre = slow;
            slow = slow->next;
            fast = fast->next->next;
        }
        pre->next = NULL;
        return merge(sortList(head), sortList(slow));
    }
    ListNode* merge(ListNode* l1, ListNode* l2) {
        if (!l1) return l2;
        if (!l2) return l1;
        if (l1->val < l2->val) {
            l1->next = merge(l1->next, l2);
            return l1;
        } else {
            l2->next = merge(l1, l2->next);
            return l2;
        }
    }
};

Java 解法二:

public class Solution {
    public ListNode sortList(ListNode head) {
        if (head == null || head.next == null) return head;
        ListNode slow = head, fast = head, pre = head;
        while (fast != null && fast.next != null) {
            pre = slow;
            slow = slow.next;
            fast = fast.next.next;
        }
        pre.next = null;
        return merge(sortList(head), sortList(slow));
    }
    public ListNode merge(ListNode l1, ListNode l2) {
        if (l1 == null) return l2;
        if (l2 == null) return l1;
        if (l1.val < l2.val) {
            l1.next = merge(l1.next, l2);
            return l1;
        } else {
            l2.next = merge(l1, l2.next);
            return l2;
        }
    }
}

Github 同步地址:

https://github.com/grandyang/leetcode/issues/148

類似題目:

Merge Two Sorted Lists

Sort Colors

Insertion Sort List

參考資料:

https://leetcode.com/problems/sort-list/description/

https://leetcode.com/problems/sort-list/discuss/46857/clean-and-short-merge-sort-solution-in-c

https://leetcode.com/problems/sort-list/discuss/46937/56ms-c-solutions-using-quicksort-with-explanations

https://leetcode.com/problems/sort-list/discuss/46772/i-have-a-pretty-good-mergesort-method-can-anyone-speed-up-the-run-time-or-reduce-the-memory-usage

到此這篇關(guān)于C++實(shí)現(xiàn)LeetCode(148.鏈表排序)的文章就介紹到這了,更多相關(guān)C++實(shí)現(xiàn)鏈表排序內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C++標(biāo)準(zhǔn)庫(kù)介紹及使用string類的詳細(xì)過程

    C++標(biāo)準(zhǔn)庫(kù)介紹及使用string類的詳細(xì)過程

    C++中將string封裝為單獨(dú)的類,string?類是?C++?標(biāo)準(zhǔn)庫(kù)中的一個(gè)非常重要的類,用于表示和操作字符串,這篇文章主要介紹了C++標(biāo)準(zhǔn)庫(kù)介紹及使用string類,需要的朋友可以參考下
    2024-08-08
  • C++ explicit關(guān)鍵字的應(yīng)用方法詳細(xì)講解

    C++ explicit關(guān)鍵字的應(yīng)用方法詳細(xì)講解

    C++ explicit關(guān)鍵字用來修飾類的構(gòu)造函數(shù),表明該構(gòu)造函數(shù)是顯式的,既然有"顯式"那么必然就有"隱式",那么什么是顯示而什么又是隱式的呢?下面就讓我們一起來看看這方面的知識(shí)吧
    2013-09-09
  • 仿寫C語言string.h頭文件檢驗(yàn)字符串函數(shù)

    仿寫C語言string.h頭文件檢驗(yàn)字符串函數(shù)

    這里給大家分享的是一個(gè)C語言string.h頭文件檢驗(yàn)字符串函數(shù)的仿寫,非常的簡(jiǎn)單實(shí)用,小編覺得這篇文寫的還不錯(cuò),希望能夠給你帶來幫助
    2021-11-11
  • C語言形參與實(shí)參使用的差別講解

    C語言形參與實(shí)參使用的差別講解

    形參出現(xiàn)在函數(shù)定義中,在整個(gè)函數(shù)體內(nèi)都可以使用, 離開該函數(shù)則不能使用。實(shí)參出現(xiàn)在主調(diào)函數(shù)中,進(jìn)入被調(diào)函數(shù)后,實(shí)參變量也不能使用,形參和實(shí)參的功能是作數(shù)據(jù)傳送。發(fā)生函數(shù)調(diào)用時(shí), 主調(diào)函數(shù)把實(shí)參的值傳送給被調(diào)函數(shù)的形參從而實(shí)現(xiàn)主調(diào)函數(shù)向被調(diào)函數(shù)的數(shù)據(jù)傳送
    2023-02-02
  • C/C++ 動(dòng)態(tài)數(shù)組的創(chuàng)建的實(shí)例詳解

    C/C++ 動(dòng)態(tài)數(shù)組的創(chuàng)建的實(shí)例詳解

    這篇文章主要介紹了C/C++ 動(dòng)態(tài)數(shù)組的創(chuàng)建的實(shí)例詳解的相關(guān)資料,希望通過本文能幫助到大家,讓大家掌握這樣的功能,需要的朋友可以參考下
    2017-10-10
  • C++中string替換所有指定字符串的方法

    C++中string替換所有指定字符串的方法

    這篇文章主要介紹了C++中string替換所有指定字符串的實(shí)例代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-05-05
  • OpenCV利用背景建模檢測(cè)運(yùn)動(dòng)物體

    OpenCV利用背景建模檢測(cè)運(yùn)動(dòng)物體

    這篇文章主要為大家詳細(xì)介紹了OpenCV利用背景建模檢測(cè)運(yùn)動(dòng)物體,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • OpenCV實(shí)現(xiàn)霍夫變換直線檢測(cè)

    OpenCV實(shí)現(xiàn)霍夫變換直線檢測(cè)

    這篇文章主要為大家詳細(xì)介紹了OpenCV實(shí)現(xiàn)霍夫變換直線檢測(cè),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • c++ 有趣的動(dòng)態(tài)轉(zhuǎn)換

    c++ 有趣的動(dòng)態(tài)轉(zhuǎn)換

    這篇文章主要介紹了c++ 動(dòng)態(tài)轉(zhuǎn)換的相關(guān)資料,幫助大家更好的理解和使用c++編程,感興趣的朋友可以了解下
    2020-09-09
  • VC實(shí)現(xiàn)批量刪除指定文件的方法

    VC實(shí)現(xiàn)批量刪除指定文件的方法

    這篇文章主要介紹了VC實(shí)現(xiàn)批量刪除指定文件的方法,是一個(gè)比較普遍且實(shí)用的功能,需要的朋友可以參考下
    2014-07-07

最新評(píng)論