@@ -79,7 +79,6 @@ class Solution:
79
79
lists[i] = self .mergeTwoLists(lists[i - 1 ], lists[i])
80
80
return lists[n - 1 ]
81
81
82
-
83
82
def mergeTwoLists (self , l1 : ListNode, l2 : ListNode) -> ListNode:
84
83
dummy = ListNode()
85
84
cur = dummy
@@ -123,7 +122,225 @@ class Solution {
123
122
}
124
123
125
124
private ListNode mergeTwoLists (ListNode l1 , ListNode l2 ) {
126
- ListNode dummy = new ListNode (0 );
125
+ ListNode dummy = new ListNode ();
126
+ ListNode cur = dummy;
127
+ while (l1 != null && l2 != null ) {
128
+ if (l1. val <= l2. val) {
129
+ cur. next = l1;
130
+ l1 = l1. next;
131
+ } else {
132
+ cur. next = l2;
133
+ l2 = l2. next;
134
+ }
135
+ cur = cur. next;
136
+ }
137
+ cur. next = l1 == null ? l2 : l1;
138
+ return dummy. next;
139
+ }
140
+ }
141
+ ```
142
+
143
+ ### ** C++**
144
+
145
+ ``` cpp
146
+ /* *
147
+ * Definition for singly-linked list.
148
+ * struct ListNode {
149
+ * int val;
150
+ * ListNode *next;
151
+ * ListNode() : val(0), next(nullptr) {}
152
+ * ListNode(int x) : val(x), next(nullptr) {}
153
+ * ListNode(int x, ListNode *next) : val(x), next(next) {}
154
+ * };
155
+ */
156
+ class Solution {
157
+ public:
158
+ ListNode* mergeKLists(vector<ListNode* >& lists) {
159
+ int n = lists.size();
160
+ if (n == 0) {
161
+ return nullptr;
162
+ }
163
+ for (int i = 1; i < n; ++i) {
164
+ lists[ i] = mergeTwoLists(lists[ i - 1] , lists[ i] );
165
+ }
166
+ return lists[ n - 1] ;
167
+ }
168
+
169
+ private:
170
+ ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
171
+ ListNode* dummy = new ListNode();
172
+ ListNode* cur = dummy;
173
+ while (l1 && l2) {
174
+ if (l1->val <= l2->val) {
175
+ cur->next = l1;
176
+ l1 = l1->next;
177
+ } else {
178
+ cur->next = l2;
179
+ l2 = l2->next;
180
+ }
181
+ cur = cur->next;
182
+ }
183
+ cur->next = l1 ? l1 : l2;
184
+ return dummy->next;
185
+ }
186
+ };
187
+ ```
188
+
189
+ ### **Go**
190
+
191
+ ```go
192
+ /**
193
+ * Definition for singly-linked list.
194
+ * type ListNode struct {
195
+ * Val int
196
+ * Next *ListNode
197
+ * }
198
+ */
199
+ func mergeKLists(lists []*ListNode) *ListNode {
200
+ n := len(lists)
201
+ if n == 0 {
202
+ return nil
203
+ }
204
+ for i := 1; i < n; i++ {
205
+ lists[i] = mergeTwoLists(lists[i-1], lists[i])
206
+ }
207
+ return lists[n-1]
208
+ }
209
+
210
+ func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
211
+ dummy := &ListNode{}
212
+ cur := dummy
213
+ for l1 != nil && l2 != nil {
214
+ if l1.Val <= l2.Val {
215
+ cur.Next = l1
216
+ l1 = l1.Next
217
+ } else {
218
+ cur.Next = l2
219
+ l2 = l2.Next
220
+ }
221
+ cur = cur.Next
222
+ }
223
+ if l1 != nil {
224
+ cur.Next = l1
225
+ } else if l2 != nil {
226
+ cur.Next = l2
227
+ }
228
+ return dummy.Next
229
+ }
230
+ ```
231
+
232
+ ### ** JavaScript**
233
+
234
+ ``` js
235
+ /**
236
+ * Definition for singly-linked list.
237
+ * function ListNode(val, next) {
238
+ * this.val = (val===undefined ? 0 : val)
239
+ * this.next = (next===undefined ? null : next)
240
+ * }
241
+ */
242
+ /**
243
+ * @param {ListNode[]} lists
244
+ * @return {ListNode}
245
+ */
246
+ var mergeKLists = function (lists ) {
247
+ const n = lists .length ;
248
+ if (n == 0 ) {
249
+ return null ;
250
+ }
251
+ for (let i = 1 ; i < n; ++ i) {
252
+ lists[i] = mergeTwoLists (lists[i - 1 ], lists[i]);
253
+ }
254
+ return lists[n - 1 ];
255
+ };
256
+
257
+ function mergeTwoLists (l1 , l2 ) {
258
+ const dummy = new ListNode ();
259
+ let cur = dummy;
260
+ while (l1 && l2) {
261
+ if (l1 .val <= l2 .val ) {
262
+ cur .next = l1;
263
+ l1 = l1 .next ;
264
+ } else {
265
+ cur .next = l2;
266
+ l2 = l2 .next ;
267
+ }
268
+ cur = cur .next ;
269
+ }
270
+ cur .next = l1 || l2;
271
+ return dummy .next ;
272
+ }
273
+ ```
274
+
275
+ ### ** Ruby**
276
+
277
+ ``` rb
278
+ # Definition for singly-linked list.
279
+ # class ListNode
280
+ # attr_accessor :val, :next
281
+ # def initialize(val = 0, _next = nil)
282
+ # @val = val
283
+ # @next = _next
284
+ # end
285
+ # end
286
+ # @ param {ListNode[]} lists
287
+ # @ return {ListNode}
288
+ def merge_k_lists (lists )
289
+ n = lists.length
290
+ i = 1
291
+ while i < n
292
+ lists[i] = merge_two_lists(lists[i - 1 ], lists[i])
293
+ i += 1
294
+ end
295
+ lists[n - 1 ]
296
+ end
297
+
298
+ def merge_two_lists (l1 , l2 )
299
+ dummy = ListNode .new ()
300
+ cur = dummy
301
+ while l1 && l2
302
+ if l1.val <= l2.val
303
+ cur.next = l1
304
+ l1 = l1.next
305
+ else
306
+ cur.next = l2
307
+ l2 = l2.next
308
+ end
309
+ cur = cur.next
310
+ end
311
+ cur.next = l1 || l2
312
+ dummy.next
313
+ end
314
+ ```
315
+
316
+ ### ** C#**
317
+
318
+ ``` cs
319
+ /**
320
+ * Definition for singly-linked list.
321
+ * public class ListNode {
322
+ * public int val;
323
+ * public ListNode next;
324
+ * public ListNode(int val=0, ListNode next=null) {
325
+ * this.val = val;
326
+ * this.next = next;
327
+ * }
328
+ * }
329
+ */
330
+ public class Solution {
331
+ public ListNode MergeKLists (ListNode [] lists ) {
332
+ int n = lists .Length ;
333
+ if (n == 0 ) {
334
+ return null ;
335
+ }
336
+ for (int i = 1 ; i < n ; ++ i ) {
337
+ lists [i ] = MergeTwoLists (lists [i - 1 ], lists [i ]);
338
+ }
339
+ return lists [n - 1 ];
340
+ }
341
+
342
+ private ListNode MergeTwoLists (ListNode l1 , ListNode l2 ) {
343
+ ListNode dummy = new ListNode ();
127
344
ListNode cur = dummy ;
128
345
while (l1 != null && l2 != null ) {
129
346
if (l1 .val <= l2 .val ) {
0 commit comments