|
61 | 61 |
|
62 | 62 | <!-- 这里可写通用的实现逻辑 -->
|
63 | 63 |
|
| 64 | +合并前后两个链表,结果放在后一个链表位置上,依次循环下去。最后返回链表数组的最后一个元素。 |
| 65 | + |
64 | 66 | <!-- tabs:start -->
|
65 | 67 |
|
66 | 68 | ### **Python3**
|
67 | 69 |
|
68 | 70 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
69 | 71 |
|
70 | 72 | ```python
|
71 |
| - |
| 73 | +# Definition for singly-linked list. |
| 74 | +# class ListNode: |
| 75 | +# def __init__(self, val=0, next=None): |
| 76 | +# self.val = val |
| 77 | +# self.next = next |
| 78 | +class Solution: |
| 79 | + def mergeKLists(self, lists: List[ListNode]) -> ListNode: |
| 80 | + n = len(lists) |
| 81 | + if n == 0: |
| 82 | + return None |
| 83 | + for i in range(n - 1): |
| 84 | + lists[i + 1] = self.mergeTwoLists(lists[i], lists[i + 1]) |
| 85 | + return lists[-1] |
| 86 | + |
| 87 | + def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode: |
| 88 | + dummy = ListNode() |
| 89 | + cur = dummy |
| 90 | + while l1 and l2: |
| 91 | + if l1.val <= l2.val: |
| 92 | + cur.next = l1 |
| 93 | + l1 = l1.next |
| 94 | + else: |
| 95 | + cur.next = l2 |
| 96 | + l2 = l2.next |
| 97 | + cur = cur.next |
| 98 | + cur.next = l1 or l2 |
| 99 | + return dummy.next |
72 | 100 | ```
|
73 | 101 |
|
74 | 102 | ### **Java**
|
75 | 103 |
|
76 | 104 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
77 | 105 |
|
78 | 106 | ```java
|
| 107 | +/** |
| 108 | + * Definition for singly-linked list. |
| 109 | + * public class ListNode { |
| 110 | + * int val; |
| 111 | + * ListNode next; |
| 112 | + * ListNode() {} |
| 113 | + * ListNode(int val) { this.val = val; } |
| 114 | + * ListNode(int val, ListNode next) { this.val = val; this.next = next; } |
| 115 | + * } |
| 116 | + */ |
| 117 | +class Solution { |
| 118 | + public ListNode mergeKLists(ListNode[] lists) { |
| 119 | + int n = lists.length; |
| 120 | + if (n == 0) { |
| 121 | + return null; |
| 122 | + } |
| 123 | + for (int i = 0; i < n - 1; ++i) { |
| 124 | + lists[i + 1] = mergeLists(lists[i], lists[i + 1]); |
| 125 | + } |
| 126 | + return lists[n - 1]; |
| 127 | + } |
| 128 | + |
| 129 | + private ListNode mergeLists(ListNode l1, ListNode l2) { |
| 130 | + ListNode dummy = new ListNode(); |
| 131 | + ListNode cur = dummy; |
| 132 | + while (l1 != null && l2 != null) { |
| 133 | + if (l1.val <= l2.val) { |
| 134 | + cur.next = l1; |
| 135 | + l1 = l1.next; |
| 136 | + } else { |
| 137 | + cur.next = l2; |
| 138 | + l2 = l2.next; |
| 139 | + } |
| 140 | + cur = cur.next; |
| 141 | + } |
| 142 | + cur.next = l1 == null ? l2 : l1; |
| 143 | + return dummy.next; |
| 144 | + } |
| 145 | +} |
| 146 | +``` |
| 147 | + |
| 148 | +### **C++** |
| 149 | + |
| 150 | +```cpp |
| 151 | +/** |
| 152 | + * Definition for singly-linked list. |
| 153 | + * struct ListNode { |
| 154 | + * int val; |
| 155 | + * ListNode *next; |
| 156 | + * ListNode() : val(0), next(nullptr) {} |
| 157 | + * ListNode(int x) : val(x), next(nullptr) {} |
| 158 | + * ListNode(int x, ListNode *next) : val(x), next(next) {} |
| 159 | + * }; |
| 160 | + */ |
| 161 | +class Solution { |
| 162 | +public: |
| 163 | + ListNode* mergeKLists(vector<ListNode*>& lists) { |
| 164 | + int n = lists.size(); |
| 165 | + if (n == 0) return nullptr; |
| 166 | + for (int i = 1; i < n; ++i) lists[i] = mergeTwoLists(lists[i - 1], lists[i]); |
| 167 | + return lists[n - 1]; |
| 168 | + } |
| 169 | + |
| 170 | +private: |
| 171 | + ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { |
| 172 | + ListNode* dummy = new ListNode(); |
| 173 | + ListNode* cur = dummy; |
| 174 | + while (l1 && l2) |
| 175 | + { |
| 176 | + if (l1->val <= l2->val) |
| 177 | + { |
| 178 | + cur->next = l1; |
| 179 | + l1 = l1->next; |
| 180 | + } |
| 181 | + else |
| 182 | + { |
| 183 | + cur->next = l2; |
| 184 | + l2 = l2->next; |
| 185 | + } |
| 186 | + cur = cur->next; |
| 187 | + } |
| 188 | + cur->next = l1 ? l1 : l2; |
| 189 | + return dummy->next; |
| 190 | + } |
| 191 | +}; |
| 192 | +``` |
| 193 | +
|
| 194 | +### **Go** |
| 195 | +
|
| 196 | +```go |
| 197 | +/** |
| 198 | + * Definition for singly-linked list. |
| 199 | + * type ListNode struct { |
| 200 | + * Val int |
| 201 | + * Next *ListNode |
| 202 | + * } |
| 203 | + */ |
| 204 | +func mergeKLists(lists []*ListNode) *ListNode { |
| 205 | + n := len(lists) |
| 206 | + if n == 0 { |
| 207 | + return nil |
| 208 | + } |
| 209 | + for i := 1; i < n; i++ { |
| 210 | + lists[i] = mergeTwoLists(lists[i-1], lists[i]) |
| 211 | + } |
| 212 | + return lists[n-1] |
| 213 | +} |
| 214 | +
|
| 215 | + func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode { |
| 216 | + dummy := &ListNode{} |
| 217 | + cur := dummy |
| 218 | + for l1 != nil && l2 != nil { |
| 219 | + if l1.Val <= l2.Val { |
| 220 | + cur.Next = l1 |
| 221 | + l1 = l1.Next |
| 222 | + } else { |
| 223 | + cur.Next = l2 |
| 224 | + l2 = l2.Next |
| 225 | + } |
| 226 | + cur = cur.Next |
| 227 | + } |
| 228 | + if l1 != nil { |
| 229 | + cur.Next = l1 |
| 230 | + } else if l2 != nil { |
| 231 | + cur.Next = l2 |
| 232 | + } |
| 233 | + return dummy.Next |
| 234 | +} |
| 235 | +``` |
| 236 | + |
| 237 | +### **JavaScript** |
| 238 | + |
| 239 | +```js |
| 240 | +/** |
| 241 | + * Definition for singly-linked list. |
| 242 | + * function ListNode(val, next) { |
| 243 | + * this.val = (val===undefined ? 0 : val) |
| 244 | + * this.next = (next===undefined ? null : next) |
| 245 | + * } |
| 246 | + */ |
| 247 | +/** |
| 248 | + * @param {ListNode[]} lists |
| 249 | + * @return {ListNode} |
| 250 | + */ |
| 251 | +var mergeKLists = function(lists) { |
| 252 | + const n = lists.length; |
| 253 | + if (n == 0) { |
| 254 | + return null; |
| 255 | + } |
| 256 | + for (let i = 1; i < n; ++i) { |
| 257 | + lists[i] = mergeTwoLists(lists[i - 1], lists[i]); |
| 258 | + } |
| 259 | + return lists[n - 1]; |
| 260 | +}; |
| 261 | + |
| 262 | +function mergeTwoLists(l1, l2) { |
| 263 | + const dummy = new ListNode(); |
| 264 | + let cur = dummy; |
| 265 | + while (l1 && l2) { |
| 266 | + if (l1.val <= l2.val) { |
| 267 | + cur.next = l1; |
| 268 | + l1 = l1.next; |
| 269 | + } else { |
| 270 | + cur.next = l2; |
| 271 | + l2 = l2.next; |
| 272 | + } |
| 273 | + cur = cur.next; |
| 274 | + } |
| 275 | + cur.next = l1 || l2; |
| 276 | + return dummy.next; |
| 277 | +} |
| 278 | +``` |
| 279 | + |
| 280 | +### **Ruby** |
| 281 | + |
| 282 | +```rb |
| 283 | +# Definition for singly-linked list. |
| 284 | +# class ListNode |
| 285 | +# attr_accessor :val, :next |
| 286 | +# def initialize(val = 0, _next = nil) |
| 287 | +# @val = val |
| 288 | +# @next = _next |
| 289 | +# end |
| 290 | +# end |
| 291 | +# @param {ListNode[]} lists |
| 292 | +# @return {ListNode} |
| 293 | +def merge_k_lists(lists) |
| 294 | + n = lists.length |
| 295 | + i = 1 |
| 296 | + while i < n |
| 297 | + lists[i] = merge_two_lists(lists[i - 1], lists[i]) |
| 298 | + i += 1 |
| 299 | + end |
| 300 | + lists[n - 1] |
| 301 | +end |
| 302 | + |
| 303 | +def merge_two_lists(l1, l2) |
| 304 | + dummy = ListNode.new() |
| 305 | + cur = dummy |
| 306 | + while l1 && l2 |
| 307 | + if l1.val <= l2.val |
| 308 | + cur.next = l1 |
| 309 | + l1 = l1.next |
| 310 | + else |
| 311 | + cur.next = l2 |
| 312 | + l2 = l2.next |
| 313 | + end |
| 314 | + cur = cur.next |
| 315 | + end |
| 316 | + cur.next = l1 || l2 |
| 317 | + dummy.next |
| 318 | +end |
| 319 | +``` |
79 | 320 |
|
| 321 | +### **C#** |
| 322 | + |
| 323 | +```cs |
| 324 | +/** |
| 325 | + * Definition for singly-linked list. |
| 326 | + * public class ListNode { |
| 327 | + * public int val; |
| 328 | + * public ListNode next; |
| 329 | + * public ListNode(int val=0, ListNode next=null) { |
| 330 | + * this.val = val; |
| 331 | + * this.next = next; |
| 332 | + * } |
| 333 | + * } |
| 334 | + */ |
| 335 | +public class Solution { |
| 336 | + public ListNode MergeKLists(ListNode[] lists) { |
| 337 | + int n = lists.Length; |
| 338 | + if (n == 0) { |
| 339 | + return null; |
| 340 | + } |
| 341 | + for (int i = 1; i < n; ++i) { |
| 342 | + lists[i] = MergeTwoLists(lists[i - 1], lists[i]); |
| 343 | + } |
| 344 | + return lists[n - 1]; |
| 345 | + } |
| 346 | + |
| 347 | + private ListNode MergeTwoLists(ListNode l1, ListNode l2) { |
| 348 | + ListNode dummy = new ListNode(); |
| 349 | + ListNode cur = dummy; |
| 350 | + while (l1 != null && l2 != null) { |
| 351 | + if (l1.val <= l2.val) { |
| 352 | + cur.next = l1; |
| 353 | + l1 = l1.next; |
| 354 | + } else { |
| 355 | + cur.next = l2; |
| 356 | + l2 = l2.next; |
| 357 | + } |
| 358 | + cur = cur.next; |
| 359 | + } |
| 360 | + cur.next = l1 == null ? l2 : l1; |
| 361 | + return dummy.next; |
| 362 | + } |
| 363 | +} |
80 | 364 | ```
|
81 | 365 |
|
82 | 366 | ### **...**
|
|
0 commit comments