溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C++怎么實現鏈表排序

發(fā)布時間:2021-07-28 19:11:29 來源:億速云 閱讀:679 作者:chen 欄目:開發(fā)技術

本篇內容主要講解“C++怎么實現鏈表排序”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“C++怎么實現鏈表排序”吧!

鏈表排序

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

常見排序方法有很多,插入排序,選擇排序,堆排序,快速排序,冒泡排序,歸并排序,桶排序等等。。它們的時間復雜度不盡相同,而這里題目限定了時間必須為O(nlgn),符合要求只有快速排序,歸并排序,堆排序,而根據單鏈表的特點,最適于用歸并排序。為啥呢?這是由于鏈表自身的特點決定的,由于不能通過坐標來直接訪問元素,所以快排什么的可能不太容易實現(但是被評論區(qū)的大神們打臉,還是可以實現的),堆排序的話,如果讓新建結點的話,還是可以考慮的,若只能交換結點,最好還是不要用。而歸并排序(又稱混合排序)因其可以利用遞歸來交換數字,天然適合鏈表這種結構。歸并排序的核心是一個 merge() 函數,其主要是合并兩個有序鏈表,這個在 LeetCode 中也有單獨的題目 Merge Two Sorted Lists。由于兩個鏈表是要有序的才能比較容易 merge,那么對于一個無序的鏈表,如何才能拆分成有序的兩個鏈表呢?我們從簡單來想,什么時候兩個鏈表一定都是有序的?就是當兩個鏈表各只有一個結點的時候,一定是有序的。而歸并排序的核心其實是分治法 Divide and Conquer,就是將鏈表從中間斷開,分成兩部分,左右兩邊再分別調用排序的遞歸函數 sortList(),得到各自有序的鏈表后,再進行 merge(),這樣整體就是有序的了。因為子鏈表的遞歸函數中還是會再次拆成兩半,當拆到鏈表只有一個結點時,無法繼續(xù)拆分了,而這正好滿足了前面所說的“一個結點的時候一定是有序的”,這樣就可以進行 merge 了。然后再回溯回去,每次得到的都是有序的鏈表,然后進行 merge,直到還原整個長度。這里將鏈表從中間斷開的方法,采用的就是快慢指針,大家可能對快慢指針找鏈表中的環(huá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) {
        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函數中也使用了遞歸,這樣使代碼更加簡潔啦~

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;
        }
    }
}

到此,相信大家對“C++怎么實現鏈表排序”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續(xù)學習!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

c++
AI