148. Sort List, O(1) 空间的做法

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

这道要求O(1)空间复杂度,这是比较难的地方。
常用的merge sort都是O(LogN)的(因为call stack)
我在评论区看到的高票答案都不是O(logN)的。
当然我搜了一下也找到确实是O(1)空间复杂度的解法,不过不容易找都被淹没了。
我这里帖一下我的。

主要思路还是merge sort,但是是bottom up 的merge sort.
用while loop从下往上找,用一个变量step来控制merge size.
初始化step为1, 每次把step * 2 直接step >= list的 size。
思路还是比较直接的。
challenging的地方在于如何写的漂亮简单。
我帖了一下我的代码, 欢迎交流。

我leetcode论坛的帖子在此,
https://leetcode.com/problems/sort-list/discuss/239384/Java-Real-O(1)-space-No-recursion-only-iterative-merge-sort-with-full-comments

public ListNode sortList(ListNode head) {
        int step = 1;
        ListNode dummyHead = new ListNode(0);
        dummyHead.next = head;
        int size = getSize(head);
        while (step < size) {
            dummyHead.next = splitAndMergeAtK(dummyHead.next, step);
            step *= 2;
        }
        return dummyHead.next;
    }
    private ListNode splitAndMergeAtK(ListNode head, int K) {
        // iterative merge for every 2 size K segment,
        ListNode dummyHead = new ListNode(0);
        ListNode cur = dummyHead;
        while (head != null) {
            ListNode[] headTail1 = getK(head, K); // Get a segment of size K
            head = headTail1[1].next;
            headTail1[1].next = null;
            if (head == null) {
                cur.next = headTail1[0];
                return dummyHead.next;
            }
            ListNode[] headTail2 = getK(head, K); // Get another segment of size K
            
            head = headTail2[1].next;
            headTail2[1].next = null;
            ListNode[] mergedHeadTail =  mergeK(headTail1[0], headTail2[0]); //merge two segments
            cur.next = mergedHeadTail[0]; // hook up
            cur = mergedHeadTail[1];
        }
        return dummyHead.next;
    }
    private ListNode[] getK(ListNode head, int K) { // get a segment of size K
        ListNode[] ans = new ListNode[2]; //head must not be null;
        ans[0] = head;
        for (int i = 0; i < K; i++) {
            ans[1] = head;
            head = head.next;
            if (head == null) break;
        }
        return ans;
    }
    private ListNode[] mergeK(ListNode head1, ListNode head2) {
        ListNode dummyHead = new ListNode(0);
        ListNode cur = dummyHead;
        while (head1 != null && head2 != null) {
            if (head1.val <= head2.val) {
                cur.next = head1;
                head1 = head1.next;
            } else {
                cur.next = head2;
                head2 = head2.next;
            }
            cur = cur.next;
            cur.next = null;
        }
        cur.next = head1 != null ? head1 : head2;
        while (cur.next != null) {
            cur = cur.next;
        }
        return new ListNode[]{dummyHead.next, cur};
    }
    private int getSize(ListNode head) {
        int count = 0;
        while (head != null) {
            count++;
            head = head.next;
        }
        return count;
    }
    原文作者:尚无花名
    原文地址: https://www.jianshu.com/p/c2ecb519a8f0
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞