1. 程式人生 > >LeetCode 23 Merge k Sorted Lists(合併K個已排序連結串列)

LeetCode 23 Merge k Sorted Lists(合併K個已排序連結串列)

翻譯

合併K個已排序的連結串列,並且將其排序並返回。
分析和描述其複雜性。

原文

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.

程式碼

我們採用分治的方法來解決這個問題,其有K個連結串列,不斷將其劃分(partition),再將其歸併(merge)。

劃分的部分並不難,將其不斷分成兩部分,但是需要注意的是可能出現start和end相等的情況,這時候就直接return lists[start]就可以了。

mid = (start + end
) / 2
start -- mid (1) mid + 1 -- end (2)

上面的(1)和(2)就不斷的替換更新,不斷作為引數寫到partition函式內。

所以結合起來就是下面這樣的:

C Plus Plus

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution { public: ListNode* mergeKLists(vector<ListNode*> &lists) { return partition(lists, 0, lists.size() - 1); } ListNode* partition(vector<ListNode*>& lists, int start, int end) { if(start == end) { return lists[start]; } if
(start < end) { int mid = (start + end) / 2; ListNode* l1 = partition(lists, start, mid); ListNode* l2 = partition(lists, mid + 1, end); return mergeTwoLists(l1, l2); } return NULL; } ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { if(l2 == NULL) return l1; if(l1 == NULL) return l2; if(l1->val > l2->val) { ListNode* temp = l2; temp->next = mergeTwoLists(l1, l2->next); return temp; } else { ListNode* temp = l1; temp->next = mergeTwoLists(l1->next, l2); return temp; } } };

Java

updated at 2016/09/21
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode mergeKLists(ListNode[] lists) {
        return partition(lists, 0, lists.length - 1);
    }

    public ListNode partition(ListNode[] lists, int start, int end) {
        if (start == end) {
            return lists[start];
        }

        if (start < end) {
            int mid = (start + end) / 2;
            ListNode l1 = partition(lists, start, mid);
            ListNode l2 = partition(lists, mid + 1, end);
            return mergeTwoLists(l1, l2);
        }

        return null;
    }

    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if (l2 == null) return l1;
        if (l1 == null) return l2;

        if (l1.val < l2.val) {
            ListNode tmp = l1;
            tmp.next = mergeTwoLists(l1.next, l2);
            return tmp;
        } else {
            ListNode tmp = l2;
            tmp.next = mergeTwoLists(l1, l2.next);
            return tmp;
        }
    }
}

繼續學習大神的的解法:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */ 
class Solution {
public:
    ListNode* mergeKLists(vector<ListNode*> &lists) {
        int size = lists.size();
        if(size == 0) return NULL;
        if(size == 1) return lists[0];

        int i = 2, j;
        while(i / 2 < size) {
            for(j = 0; j < size; j += i) {
                ListNode* p = lists[j];
                if(j + i / 2 < size) {
                    p = mergeTwoLists(p, lists[j + i / 2]);
                    lists[j] = p;
                }
            }
            i *= 2;
        }
        return lists[0];
    }

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

        if(l1->val > l2->val) {
            ListNode* temp = l2;
            temp->next = mergeTwoLists(l1, l2->next);
            return temp;
        } else {
            ListNode* temp = l1;
            temp->next = mergeTwoLists(l1->next, l2);
            return temp;
        }
    }
};