Algorithm:合并K个升序链表

74 篇文章 2 订阅

 /***

 

给你一个链表数组,每个链表都已经按升序排列。

请你将所有链表合并到一个升序链表中,返回合并后的链表。

示例 1:

输入:lists = [[1,4,5],[1,3,4],[2,6]]
输出:[1,1,2,3,4,4,5,6]
解释:链表数组如下:
[
  1->4->5,
  1->3->4,
  2->6
]
将它们合并到一个有序链表中得到。
1->1->2->3->4->4->5->6
示例 2:

输入:lists = []
输出:[]
示例 3:

输入:lists = [[]]
输出:[]
 

提示:

k == lists.length
0 <= k <= 10^4
0 <= lists[i].length <= 500
-10^4 <= lists[i][j] <= 10^4
lists[i] 按 升序 排列
lists[i].length 的总和不超过 10^4

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/merge-k-sorted-lists
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

***/
解题思路:

/**
 * Definition for singly-linked list.
 */

#include <iostream>
#include <vector>

using namespace std;

struct ListNode {
    int val;
    ListNode *next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode *next) : val(x), next(next) {}
};

class Solution {
public:
    ListNode* mergeKLists(vector<ListNode*>& lists) {
        ListNode* head = nullptr;
        int listsNumber = lists.size();

        if(0 == listsNumber)
        {
            return head;
        }

        ListNode* node = nullptr;
        int minValue = 0;
        int listIndex = 0;
        bool firstAssign = true;
        bool newValFind = true;

        while(newValFind)
        {
            firstAssign = true;
            newValFind = false;

            for(int i = 0; i < listsNumber; i++)
            {
                if(nullptr != lists[i])
                {
                    newValFind = true;
                    cout << "Index: " << i << " Value: " << lists[i]->val << endl;

                    if(firstAssign)
                    {
                        minValue = lists[i]->val;
                        listIndex = i;
                        firstAssign = false;

                        cout << "First assign listIndex: " << listIndex << " minValue: " << minValue << endl;
                    }
                    else
                    {
                        if(lists[i]->val < minValue)
                        {
                            minValue = lists[i]->val;
                            listIndex = i;
                        }

                        cout << "After assign listIndex: " << listIndex << " minValue: " << minValue << endl;
                    }
                }
            }

            cout << "After loop listIndex: " << listIndex << " minValue: " << minValue << endl;

            if(newValFind)
            {
                if(nullptr == head)
                {
                    head = lists[listIndex];
                    node = head;
                }
                else
                {
                    node->next = lists[listIndex];
                    node = node->next;
                }

                lists[listIndex] = lists[listIndex]->next;
            }
        }

        return head;
    }
};

int main()
{
    Solution sol;

    ListNode node_1(1);
    ListNode node_2(4);
    ListNode node_3(5);

    node_1.next = &node_2;
    node_2.next = &node_3;

    ListNode node_4(1);
    ListNode node_5(3);
    ListNode node_6(4);

    node_4.next = &node_5;
    node_5.next = &node_6;

    vector<ListNode*> lists;
    lists.push_back(&node_1);
    lists.push_back(&node_4);

    ListNode* result = sol.mergeKLists(lists);

    while(nullptr != result)
    {
        cout << result->val << endl;
        result = result->next;
    }

    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AllenSun-1990

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值