@@ -209,8 +209,8 @@ function mergeTwoLists(
209
209
l1 : ListNode | null ,
210
210
l2 : ListNode | null ,
211
211
): ListNode | null {
212
- const res = new ListNode ();
213
- let cur = res ;
212
+ const duumy = new ListNode ();
213
+ let cur = duumy ;
214
214
while (l1 && l2 ) {
215
215
let node: ListNode ;
216
216
if (l1 .val < l2 .val ) {
@@ -224,7 +224,36 @@ function mergeTwoLists(
224
224
cur = node ;
225
225
}
226
226
cur .next = l1 || l2 ;
227
- return res .next ;
227
+ return duumy .next ;
228
+ }
229
+ ```
230
+
231
+ ``` ts
232
+ /**
233
+ * Definition for singly-linked list.
234
+ * class ListNode {
235
+ * val: number
236
+ * next: ListNode | null
237
+ * constructor(val?: number, next?: ListNode | null) {
238
+ * this.val = (val===undefined ? 0 : val)
239
+ * this.next = (next===undefined ? null : next)
240
+ * }
241
+ * }
242
+ */
243
+
244
+ function mergeTwoLists(
245
+ l1 : ListNode | null ,
246
+ l2 : ListNode | null ,
247
+ ): ListNode | null {
248
+ if (l1 == null || l2 == null ) {
249
+ return l1 || l2 ;
250
+ }
251
+ if (l1 .val < l2 .val ) {
252
+ l1 .next = mergeTwoLists (l1 .next , l2 );
253
+ return l1 ;
254
+ }
255
+ l2 .next = mergeTwoLists (l1 , l2 .next );
256
+ return l2 ;
228
257
}
229
258
```
230
259
@@ -255,10 +284,10 @@ impl Solution {
255
284
match (l1 , l2 ) {
256
285
(Some (mut n1 ), Some (mut n2 )) => {
257
286
if n1 . val < n2 . val {
258
- n1 . next = Solution :: merge_two_lists (n1 . next, Some (n2 ));
287
+ n1 . next = Self :: merge_two_lists (n1 . next, Some (n2 ));
259
288
Some (n1 )
260
289
} else {
261
- n2 . next = Solution :: merge_two_lists (Some (n1 ), n2 . next);
290
+ n2 . next = Self :: merge_two_lists (Some (n1 ), n2 . next);
262
291
Some (n2 )
263
292
}
264
293
}
@@ -270,6 +299,55 @@ impl Solution {
270
299
}
271
300
```
272
301
302
+ ``` rust
303
+ // Definition for singly-linked list.
304
+ // #[derive(PartialEq, Eq, Clone, Debug)]
305
+ // pub struct ListNode {
306
+ // pub val: i32,
307
+ // pub next: Option<Box<ListNode>>
308
+ // }
309
+ //
310
+ // impl ListNode {
311
+ // #[inline]
312
+ // fn new(val: i32) -> Self {
313
+ // ListNode {
314
+ // next: None,
315
+ // val
316
+ // }
317
+ // }
318
+ // }
319
+ impl Solution {
320
+ pub fn merge_two_lists (
321
+ mut l1 : Option <Box <ListNode >>,
322
+ mut l2 : Option <Box <ListNode >>,
323
+ ) -> Option <Box <ListNode >> {
324
+ match (l1 . is_some (), l2 . is_some ()) {
325
+ (false , false ) => None ,
326
+ (true , false ) => l1 ,
327
+ (false , true ) => l2 ,
328
+ (true , true ) => {
329
+ let mut dummy = Box :: new (ListNode :: new (0 ));
330
+ let mut cur = & mut dummy ;
331
+ while l1 . is_some () && l2 . is_some () {
332
+ cur . next = if l1 . as_ref (). unwrap (). val < l2 . as_ref (). unwrap (). val {
333
+ let mut res = l1 . take ();
334
+ l1 = res . as_mut (). unwrap (). next. take ();
335
+ res
336
+ } else {
337
+ let mut res = l2 . take ();
338
+ l2 = res . as_mut (). unwrap (). next. take ();
339
+ res
340
+ };
341
+ cur = cur . next. as_mut (). unwrap ();
342
+ }
343
+ cur . next = if l1 . is_some () { l1 . take () } else { l2 . take () };
344
+ dummy . next. take ()
345
+ }
346
+ }
347
+ }
348
+ }
349
+ ```
350
+
273
351
### ** ...**
274
352
275
353
```
0 commit comments