148. Sort List

Description

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

Solution

Merge sort

使用歸并排序思想做這道題還是蠻簡(jiǎn)單的。

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode sortList(ListNode head) {
        return mergeSort(head);
    }
    
    public ListNode mergeSort(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        
        ListNode slow = head;
        ListNode fast = head.next;
        
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        
        ListNode head2 = slow.next;
        slow.next = null;
        
        head = mergeSort(head);
        head2 = mergeSort(head2);
        
        return mergeSortedList(head, head2);
    }
    
    public ListNode mergeSortedList(ListNode p, ListNode q) {
        ListNode dummy = new ListNode(0);
        ListNode tail = dummy;
        
        while (p != null && q != null) {
            if (p.val <= q.val) {
                tail.next = p;
                p = p.next;
            } else {
                tail.next = q;
                q = q.next;
            }
            
            tail = tail.next;
        }
        
        if (p != null) {
            tail.next = p;
        } else {
            tail.next = q;
        }
        
        return dummy.next;
    }
}

Quick sort(TLE)

雖然會(huì)超時(shí)但是寫起來也蠻有意思的。pivot自然選擇head節(jié)點(diǎn),比較tricky的地方是如何去移動(dòng)節(jié)點(diǎn)。好實(shí)現(xiàn)的方式是將小于pivot的移動(dòng)到當(dāng)前鏈表的頭部,但注意需要一個(gè)prev節(jié)點(diǎn)記錄head之前的節(jié)點(diǎn)(防止鏈表被弄斷?。?,然后將節(jié)點(diǎn)插入到prev和firstHead之間。

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode sortList(ListNode head) {
        return quickSort(null, head, null);
    }

    public ListNode quickSort(ListNode prev, ListNode head, ListNode tail) {
        if (head == tail || head.next == tail) {
            return head;
        }

        ListNode fhead = head;
        ListNode curr = head;

        // compare curr.next not curr so we could spare a pre pointer
        while (curr.next != tail) { 
            if (curr.next.val < head.val) {     
                // move p to the between of prev and fhead
                ListNode p = curr.next;
                curr.next = p.next;
                p.next = fhead;
                if (prev != null) {
                    prev.next = p;
                }
                fhead = p;
            } else {
                curr = curr.next;
            }
        }

        quickSort(head, head.next, tail);
        return quickSort(prev, fhead, head);
    }
}
最后編輯于
?著作權(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),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • LeetCode 148 Sort List Sort a linked list in O(n log n) t...
    ShuiLocked閱讀 752評(píng)論 0 0
  • Sort a linked list in O(n log n) time using constant spac...
    ShutLove閱讀 286評(píng)論 0 0
  • O(n log n) time 的要求,可以參與merge sort 尋找中間節(jié)點(diǎn)的時(shí)候,我們不是需要找到的中間節(jié)...
    larrymusk閱讀 350評(píng)論 0 0
  • 原題 在 O(n log n) 時(shí)間復(fù)雜度和常數(shù)級(jí)的空間復(fù)雜度下給鏈表排序。 樣例給出 1->3->2->null...
    Jason_Yuan閱讀 813評(píng)論 0 0
  • Though, up to now, the microfinance in India is quite suc...
    雷默君閱讀 419評(píng)論 0 1

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