23. Merge k Sorted Lists

来源:互联网 发布:最简单的php爬虫 编辑:程序博客网 时间:2024/06/05 06:02

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



本人的思想:就是从第一条linkedlist逐渐和下面的linkedlist合并。

/** * Definition for singly-linked list. * struct ListNode { *     int val; *     struct ListNode *next; * }; */struct ListNode* mergeKLists(struct ListNode** lists, int listsSize) {    if (listsSize == 0) {        return NULL;    }    struct ListNode * m_l = lists[0];    struct ListNode * prev = NULL;    int i;    for (i = 1; i < listsSize; ++i) {        struct ListNode *indx_1 = m_l;        struct ListNode *indx_2 = lists[i];        // merge this 2 sorted linkedlist        if(indx_1 == NULL) {            m_l = indx_2;        } else if (indx_2 == NULL) {            m_l = indx_1;        } else {            if(indx_1->val <= indx_2->val) {                m_l = indx_1;                prev = m_l;                indx_1 = indx_1->next;            } else {                m_l = indx_2;                indx_2 = indx_2->next;                m_l->next = indx_1;                prev = m_l;            }            while(indx_1 && indx_2) {                if (indx_1->val <= indx_2->val) {                    prev = indx_1;                    indx_1 = indx_1->next;                } else {                    prev->next = indx_2;                    prev = indx_2;                    indx_2 = indx_2->next;                    prev->next = indx_1;                }            }            if(indx_2) {                prev->next = indx_2;            }        }    }    return m_l;}

这里还有一种利用mergesort方法,挺高效的

/** * Definition for singly-linked list. * struct ListNode { *     int val; *     struct ListNode *next; * }; */struct ListNode *merge_lists (struct ListNode *a, struct ListNode *b) {    struct ListNode *head, *tail, *tmp;    head = tail = NULL;    while (a && b) {        if (a->val < b->val) {            tmp = a;            a = a->next;        } else {            tmp = b;            b = b->next;        }        tmp->next = NULL;        if (!tail) {            head = tmp;            tail = tmp;        } else {            tail->next = tmp;            tail = tmp;        }    }         if (a) {        if (!tail) {            head = a;            tail = a;        } else {            tail->next = a;            tail = a;        }    }    if (b) {        if (!tail) {            head = b;            tail = b;        } else {            tail->next = b;            tail = b;        }    }    return head;}struct ListNode* mergeKLists(struct ListNode** lists, int listsSize) {    int mid;    struct ListNode *first_half;    struct ListNode *second_half;         if (listsSize == 0) return NULL;    if (listsSize == 1) return lists[0];    if (listsSize == 2) return merge_lists(lists[0], lists[1]);        mid = listsSize/2;    first_half = mergeKLists(lists, mid);    second_half = mergeKLists(lists+mid, listsSize-mid);    return merge_lists(first_half, second_half);}





原创粉丝点击