leetcode:23. Merge k Sorted Lists

来源:互联网 发布:多媒体管理系统 源码 编辑:程序博客网 时间:2024/05/05 10:45

描述

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

思路

思路一:使用分治法的思想,每次合并两条链表
思路二:将所有链表,连成一个链表,然后将这个链使用快速排序重排一下。没有实现

代码

/** * 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) {        if(lists.empty())            return nullptr;        while(lists.size() > 1){            lists.push_back(mergeTwoLists(lists[0], lists[1]));            lists.erase(lists.begin());            lists.erase(lists.begin());        }        return lists.front();    }    ListNode* mergeTwoLists(ListNode* list1, ListNode* list2){        if(list1  == NULL)            return list2;        else if (list2 == NULL)            return list1;        else if ( list1->val <= list2->val){            list1->next = mergeTwoLists(list1->next, list2);            return list1;        }        else{            list2->next = mergeTwoLists(list1, list2->next);            return list2;        }    }};

结果

他山之玉

C O(n) solutioin

/** * Definition for singly-linked list. * struct ListNode { *     int val; *     struct ListNode *next; * }; */void MIN_HEAP_SORT(struct ListNode **lists, int index_i,int size){    int left = index_i*2 + 1;    int right= index_i*2 + 2;    if(left>=size)        return;    int min;    if(right>=size)        min = left;    else        min = lists[left]->val<lists[right]->val?left:right;    if(lists[index_i]->val>lists[min]->val){        struct ListNode *temp = lists[index_i];        lists[index_i] = lists[min];        lists[min] = temp;        MIN_HEAP_SORT(lists,min,size);    }}void BuildHeap(struct ListNode **lists,int size){    for(int i=(size-1)/2;i>=0;--i){        MIN_HEAP_SORT(lists,i,size);    }}struct ListNode *mergeKLists(struct ListNode *lists[], int k) {    if(k==0)        return NULL;//1    struct ListNode *head = (struct ListNode*)malloc(sizeof(struct ListNode));    struct ListNode *int_max = (struct ListNode*)malloc(sizeof(struct ListNode));    int_max->val = INT_MAX;    int_max->next = NULL;    struct ListNode *travel = head;    for(int i=0;i<k;++i){        if(lists[i]==NULL)            lists[i] = int_max;    }/*remove those NULL ptr*/    BuildHeap(lists,k);    while(lists[0]!=int_max){        travel->next = lists[0];        travel = lists[0];        lists[0] = lists[0]->next;        if(lists[0]==NULL)            lists[0] = int_max;        MIN_HEAP_SORT(lists,0,k);    }    travel->next = NULL;    return head->next;}

这个采用堆排序,运行的时间很短,很漂亮!!!!值得研究

Java O(n) solution

public ListNode mergeKLists(ListNode[] lists) {    if(lists.length == 0)        return null;    return mergeK(lists, 0, lists.length-1);}private ListNode mergeK(ListNode[] lists, int low, int high) {    if(low == high)        return lists[low];    if(low + 1 == high)        return mergeTwo(lists[low], lists[high]);    int mid = low + (high - low) / 2;    return mergeTwo(mergeK(lists, low, mid), mergeK(lists, mid+1, high));}private ListNode mergeTwo(ListNode l1, ListNode l2) {    if(l1 == null)        return l2;    if(l2 == null)        return l1;    ListNode head, it;    if(l1.val <= l2.val) {        head = l1;        l1 = l1.next;    } else {        head = l2;        l2 = l2.next;    }    it = head;    while(l1 != null || l2 != null) {        if(l1 != null && l2 != null) {            if(l1.val <= l2.val) {                it.next = l1;                l1 = l1.next;            } else {                it.next = l2;                l2 = l2.next;            }        } else if(l1 != null) {            it.next = l1;            l1 = l1.next;        } else {            it.next = l2;            l2 = l2.next;        }        it = it.next;    }    return head;}

python O(n) solution

from operator import attrgetterclass Solution:    # @param a list of ListNode    # @return a ListNode    def mergeKLists(self, lists):        sorted_list = []        for head in lists:            curr = head            while curr is not None:                sorted_list.append(curr)                curr = curr.next        sorted_list = sorted(sorted_list, key=attrgetter('val'))        for i, node in enumerate(sorted_list):            try:                node.next = sorted_list[i + 1]            except:                node.next = None        if sorted_list:            return sorted_list[0]        else:            return None

python的这个人的思路是将所有的lists放到连成一个总的list,然后再用排序算法自动排序。

0 0
原创粉丝点击