23. Merge k Sorted Lists

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);
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值