solution.md 5.8 KB
Newer Older
1 2
# 合并K个升序链表
<p>给你一个链表数组,每个链表都已经按升序排列。</p><p>请你将所有链表合并到一个升序链表中,返回合并后的链表。</p><p>&nbsp;</p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>lists = [[1,4,5],[1,3,4],[2,6]]<strong><br />输出:</strong>[1,1,2,3,4,4,5,6]<strong><br />解释:</strong>链表数组如下:[  1-&gt;4-&gt;5,  1-&gt;3-&gt;4,  2-&gt;6]将它们合并到一个有序链表中得到。1-&gt;1-&gt;2-&gt;3-&gt;4-&gt;4-&gt;5-&gt;6</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>lists = []<strong><br />输出:</strong>[]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>lists = [[]]<strong><br />输出:</strong>[]</pre><p>&nbsp;</p><p><strong>提示:</strong></p><ul>	<li><code>k == lists.length</code></li>	<li><code>0 &lt;= k &lt;= 10^4</code></li>	<li><code>0 &lt;= lists[i].length &lt;= 500</code></li>	<li><code>-10^4 &lt;= lists[i][j] &lt;= 10^4</code></li>	<li><code>lists[i]</code><strong>升序</strong> 排列</li>	<li><code>lists[i].length</code> 的总和不超过 <code>10^4</code></li></ul>
每日一练社区's avatar
fix bug  
每日一练社区 已提交
3
<p>以下错误的选项是?</p>
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;

struct ListNode
{
    int val;
    struct ListNode *next;
    ListNode() : val(0), next(nullptr){};
    ListNode(int x) : val(x), next(nullptr){};
    ListNode(int x, ListNode *next) : val(x), next(next){};
};
```
### after
```cpp
int main()
{
    Solution sol;

    ListNode *L1 = new ListNode;
    ListNode *l11 = new ListNode(1);
    ListNode *l12 = new ListNode(4);
    ListNode *l13 = new ListNode(5);

    L1->next = l11;
    l11->next = l12;
    l12->next = l13;

    ListNode *L2 = new ListNode;
    ListNode *l21 = new ListNode(1);
    ListNode *l22 = new ListNode(3);
    ListNode *l23 = new ListNode(4);

    L2->next = l21;
    l21->next = l22;
    l22->next = l23;

    ListNode *L3 = new ListNode;
    ListNode *l31 = new ListNode(2);
    ListNode *l32 = new ListNode(6);

    L3->next = l31;
    l31->next = l32;

    ListNode *ret = new ListNode;

    vector<ListNode *> lists = {L1, L2, L3};

    ret = sol.mergeKLists(lists);

    ListNode *p = ret->next;

    while (p != NULL)
    {
        cout << p->val << " ";
        p = p->next;
    }
    return 0;
}
```

## 答案
```cpp
struct cmp
{
    bool operator()(ListNode *a, ListNode *b)
    {
        return a->val > b->val;
    }
};
class Solution
{
public:
    ListNode *mergeKLists(vector<ListNode *> &lists)
    {
        priority_queue<ListNode *, vector<ListNode *>, cmp> queue;
        for (int i = 0; i < lists.size(); i++)
        {
            if (lists[i] != NULL)
                queue.push(lists[i]);
        }
        if (queue.empty())
            return NULL;
        ListNode *root = new ListNode(-1);
        ListNode *node;
        ListNode *lastNode = root;
        while (!queue.empty())
        {
            node = queue.top();
            queue.pop();
            lastNode->next = node;
            lastNode = lastNode->next;
            if (node->next)
                queue.push(node->next);
        }
        lastNode->next = NULL;
        return root->next;
    }
};
```
## 选项

### A
```cpp
class Solution
{
public:
    struct Status
    {
        int val;
        ListNode *ptr;

        bool operator<(const Status &rhs) const
        {
            return val > rhs.val;
        }
    };

    priority_queue<Status> q;

    ListNode *mergeKLists(vector<ListNode *> &lists)
    {
        for (int i = 0; i < lists.size() - 1; i++)
        {
            lists[i + 1] = lists[i + 1]->next;
        }
        for (auto node : lists)
        {
            if (node)
                q.push({node->val, node});
        }

        ListNode head, *tail = &head;
        while (!q.empty())
        {

            auto f = q.top();

            q.pop();

            tail->next = f.ptr;

            tail = tail->next;

            if (f.ptr->next)
                q.push({f.ptr->next->val, f.ptr->next});
        }

        return head.next;
    }
};
```

### B
```cpp
class Solution
{
public:
    ListNode *mergeTwoLists(ListNode *l1, ListNode *l2)
    {
        if (!l1 || !l2)
            return l1 ? l1 : l2;
        ListNode fake, *la = l1, *lb = l2;
        ListNode *cur = &fake;
        while (la && lb)
        {
            if (la->val < lb->val)
            {
                cur->next = la;
                la = la->next;
            }
            else
            {
                cur->next = lb;
                lb = lb->next;
            }
            cur = cur->next;
        }
        cur->next = la ? la : lb;
        return fake.next;
    }

    ListNode *mergeKLists(vector<ListNode *> &lists)
    {

        ListNode *ans = nullptr;
        for (size_t i = 0; i < lists.size(); ++i)
        {
            if (i >= 1)
                lists[i] = lists[i]->next;
            ans = mergeTwoLists(ans, lists[i]);
        }
        return ans;
    }
};

```

### C
```cpp
class Solution
{
public:
    ListNode *mergeTlists(ListNode *l1, ListNode *l2)
    {
        if (l1 == nullptr)
            return l2;
        else if (l2 == nullptr)
            return l1;
        else if (l1->val < l2->val)
        {
            l1->next = mergeTlists(l1->next, l2);
            return l1;
        }
        else
        {
            l2->next = mergeTlists(l2->next, l1);
            return l2;
        }
    }
    ListNode *mergeKLists(vector<ListNode *> &lists)
    {
        int len = lists.size();
        if (len == 0)
            return nullptr;
        if (len == 1)
            return lists[0];
        for (int i = 0; i < len - 1; i++)
        {
            lists[i + 1] = lists[i + 1]->next;

            lists[i + 1] = mergeTlists(lists[i], lists[i + 1]);
        }
        return lists[len - 1];
    }
};
```