1. 程式人生 > >[LeetCode] 023. Merge k Sorted Lists (Hard) (C++/Java/Python)

[LeetCode] 023. Merge k Sorted Lists (Hard) (C++/Java/Python)

023. Merge k Sorted Lists (Hard)

連結

題意

分析

很明顯可以想到利用已經完成的 Merge Two Sorted Lists 的函式來用。
這時有兩種方法:
1. (C++) 用二分的思想,把每個 List 和它相鄰的 List 進行 Merge,這樣規模就縮小了一半了,再重複這樣,就可以 O(nklogk) 完成。比如: [1, 2, …, n] 的第一輪 Merge 是 [1, n/2], [2, n/2+1], …
2. (Python) 也是用二分的思想,就是把 Lists 分為兩部分,分別遞迴 Merge k Sorted Lists 後變成兩個 List ,然後再對這兩 List 進行 Merge Two Sorted Lists 。

這兩種方法都是遞迴呼叫,都可以進行記憶化,用空間換時間,不過我不清楚會不會超空間(Memory Limit Exceed),所以就沒試了~

除了用二分的思路,還有更好寫的方法,就是用堆(heap),具體就是用優先佇列(Priority Queue)。
(Java) 先把每個 List 的第一個節點放進優先佇列,每次取出佇列中的最大值節點,再把那個節點的 next 放進去。

程式碼

C++:

class Solution {
public:
    ListNode *mergeKLists(vector<ListNode *> &lists) {
		int sz = lists.size();
		if (sz == 0)
			return NULL;

		while (sz > 1) {
			int k = (sz + 1) / 2;
			for (int i = 0; i < sz / 2; i++)
				lists[i] = mergeTwoLists(lists[i], lists[i + k]);
			sz = k;
		}
		return lists[0];
	}

    ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
		if (l1 == NULL)
			return l2;
		if (l2 == NULL)
			return l1;

		ListNode *start, *p1;

		if (l1->val < l2->val) {
			p1 = start = l1;
			l1 = l1->next;
		} else {
			p1 = start = l2;
			l2 = l2->next;
		}
		while (l1 != NULL && l2 != NULL) {
			if (l1->val < l2->val) {
				p1->next = l1;
				p1 = l1;
				l1 = l1->next;
			} else {
				p1->next = l2;
				p1 = l2;
				l2 = l2->next;
			}
		}
		if (l1 != NULL)
			p1->next = l1;
		else
			p1->next = l2;
		return start;
    }
};


Java:

public class Solution {

    public ListNode mergeKLists(List<ListNode> lists) {
        Queue<ListNode> heap = new PriorityQueue<ListNode>(new Comparator<ListNode>(){
            @Override public int compare(ListNode l1, ListNode l2) {
                return l1.val - l2.val;
            }
        });

        ListNode dummy = new ListNode(0), cur = dummy, tmp;
        for (ListNode list : lists) {
            if (list != null) {
                heap.offer(list);
            }
        }
        while (!heap.isEmpty()) {
            tmp = heap.poll();
            cur.next = tmp;
            cur = cur.next;
            if (tmp.next != null) {
                heap.offer(tmp.next);
            }
        }
        return dummy.next;
    }
}


Python:

class Solution:
    # @param a list of ListNode
    # @return a ListNode
    def mergeKLists(self, lists):
        if len(lists) == 0:
            return None
        if len(lists) == 1:
            return lists[0]

        mid = len(lists) // 2
        left = self.mergeKLists(lists[:mid])
        right = self.mergeKLists(lists[mid:])

        # merge left and right
        dummy = ListNode(0)
        cur = dummy
        while left or right:
            if right == None or (left and left.val <= right.val):
                cur.next = left
                left = left.next
            else:
                cur.next = right
                right = right.next
            cur = cur.next

        return dummy.next