solution.md 5.0 KB
Newer Older
1 2
# 合并两个有序链表
<p>将两个升序链表合并为一个新的 <strong>升序</strong> 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 </p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0021.Merge%20Two%20Sorted%20Lists/images/merge_ex1.jpg" style="width: 662px; height: 302px;" /><pre><strong>输入:</strong>l1 = [1,2,4], l2 = [1,3,4]<strong><br />输出:</strong>[1,1,2,3,4,4]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>l1 = [], l2 = []<strong><br />输出:</strong>[]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>l1 = [], l2 = [0]<strong><br />输出:</strong>[0]</pre><p> </p><p><strong>提示:</strong></p><ul>	<li>两个链表的节点数目范围是 <code>[0, 50]</code></li>	<li><code>-100 <= Node.val <= 100</code></li>	<li><code>l1</code> 和 <code>l2</code> 均按 <strong>非递减顺序</strong> 排列</li></ul>
每日一练社区's avatar
fix bug  
每日一练社区 已提交
3
<p>以下错误的选项是?</p>
F
fix bug  
feilong 已提交
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
## 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(2);
    ListNode *l13 = new ListNode(4);

    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 *ret = new ListNode;

    ret = sol.mergeTwoLists(L1, L2);
    ListNode *p = ret->next;

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

```

## 答案
```cpp
class Solution
{
public:
    ListNode *mergeTwoLists(ListNode *l1, ListNode *l2)
    {
        if (l1 == NULL)
            return l2;
        else if (l2 == NULL)
            return l1;
        else if (l1->val < l2->val)
        {
            l1->next = mergeTwoLists(l1->next, l2);
            return l1;
        }
        else
        {
            l2->next = mergeTwoLists(l1, l2->next);
            return l2;
        }
    }
};
```
## 选项

### A
```cpp
class Solution
{
public:
    ListNode *mergeTwoLists(ListNode *l1, ListNode *l2)
    {
        l2 = l2->next;
        ListNode *h = new ListNode(0);
        ListNode *head = h;
        while (l1 && l2)
        {
            if (l1->val < l2->val)
            {
                h->next = new ListNode(l1->val);
                h = h->next;
                l1 = l1->next;
            }
            else
            {
                h->next = new ListNode(l2->val);
                h = h->next;
                l2 = l2->next;
            }
        }
        if (l1)
        {
            while (l1)
            {
                h->next = new ListNode(l1->val);
                h = h->next;
                l1 = l1->next;
            }
        }
        else if (l2)
        {
            while (l2)
            {
                h->next = new ListNode(l2->val);
                h = h->next;
                l2 = l2->next;
            }
        }
        ListNode *ptrDelete = head;
        head = head->next;
        delete ptrDelete;
        return head;
    }
};
```

### B
```cpp
class Solution
{
public:
    ListNode *mergeTwoLists(ListNode *l1, ListNode *l2)
    {
        if (NULL == l1)
            return l2;
        if (NULL == l2)
            return l1;
        l1 = l1->next;

        ListNode *p1 = l1;
        ListNode *p2 = l2;

        ListNode *head = new ListNode(-1);
        ListNode *curNode = head;

        while (NULL != p1 && NULL != p2)
        {
            if (p1->val < p2->val)
            {
                curNode->next = p1;
                curNode = p1;
                p1 = p1->next;
            }
            else
            {
                curNode->next = p2;
                curNode = p2;
                p2 = p2->next;
            }
        }

        if (NULL != p1)
            curNode->next = p1;
        if (NULL != p2)
            curNode->next = p2;

        return head->next;
    }
};
```

### C
```cpp
class Solution
{
public:
    ListNode *mergeTwoLists(ListNode *l1, ListNode *l2)
    {
        vector<int> vec;
        l1 = l1->next;
        while (l1 != NULL)
        {
            vec.push_back(l1->val);
            l1 = l1->next;
        }
        while (l2 != NULL)
        {
            vec.push_back(l2->val);
            l2 = l2->next;
        }

        sort(vec.begin(), vec.end());
        auto head = new ListNode;
        auto cur = head;
        for (const auto &c : vec)
        {
            auto p = new ListNode(c);
            cur->next = p;
            cur = p;
        }
        return head->next;
    }
};
```