Skip to content

Commit 1788ebe

Browse files
committed
feat: add solutions to lc problem: No.0021
No.0021.Merge Two Sorted Lists
1 parent 7a153f6 commit 1788ebe

File tree

4 files changed

+382
-0
lines changed

4 files changed

+382
-0
lines changed

solution/0000-0099/0021.Merge Two Sorted Lists/README.md

+159
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,165 @@ public class Solution {
275275
}
276276
```
277277

278+
### **TypeScript**
279+
280+
递归:
281+
282+
```ts
283+
/**
284+
* Definition for singly-linked list.
285+
* class ListNode {
286+
* val: number
287+
* next: ListNode | null
288+
* constructor(val?: number, next?: ListNode | null) {
289+
* this.val = (val===undefined ? 0 : val)
290+
* this.next = (next===undefined ? null : next)
291+
* }
292+
* }
293+
*/
294+
295+
function mergeTwoLists(
296+
list1: ListNode | null,
297+
list2: ListNode | null,
298+
): ListNode | null {
299+
if (list1 == null || list2 == null) {
300+
return list1 || list2;
301+
}
302+
if (list1.val < list2.val) {
303+
list1.next = mergeTwoLists(list1.next, list2);
304+
return list1;
305+
} else {
306+
list2.next = mergeTwoLists(list1, list2.next);
307+
return list2;
308+
}
309+
}
310+
```
311+
312+
循环:
313+
314+
```ts
315+
/**
316+
* Definition for singly-linked list.
317+
* class ListNode {
318+
* val: number
319+
* next: ListNode | null
320+
* constructor(val?: number, next?: ListNode | null) {
321+
* this.val = (val===undefined ? 0 : val)
322+
* this.next = (next===undefined ? null : next)
323+
* }
324+
* }
325+
*/
326+
327+
function mergeTwoLists(
328+
list1: ListNode | null,
329+
list2: ListNode | null,
330+
): ListNode | null {
331+
const dummy = new ListNode(0);
332+
let cur = dummy;
333+
while (list1 != null && list2 != null) {
334+
if (list1.val < list2.val) {
335+
cur.next = list1;
336+
list1 = list1.next;
337+
} else {
338+
cur.next = list2;
339+
list2 = list2.next;
340+
}
341+
cur = cur.next;
342+
}
343+
cur.next = list1 || list2;
344+
return dummy.next;
345+
}
346+
```
347+
348+
### **Rust**
349+
350+
递归:
351+
352+
```rust
353+
// Definition for singly-linked list.
354+
// #[derive(PartialEq, Eq, Clone, Debug)]
355+
// pub struct ListNode {
356+
// pub val: i32,
357+
// pub next: Option<Box<ListNode>>
358+
// }
359+
//
360+
// impl ListNode {
361+
// #[inline]
362+
// fn new(val: i32) -> Self {
363+
// ListNode {
364+
// next: None,
365+
// val
366+
// }
367+
// }
368+
// }
369+
impl Solution {
370+
pub fn merge_two_lists(
371+
list1: Option<Box<ListNode>>,
372+
list2: Option<Box<ListNode>>,
373+
) -> Option<Box<ListNode>> {
374+
match (list1, list2) {
375+
(None, None) => None,
376+
(Some(list), None) => Some(list),
377+
(None, Some(list)) => Some(list),
378+
(Some(mut list1), Some(mut list2)) => {
379+
if list1.val < list2.val {
380+
list1.next = Self::merge_two_lists(list1.next, Some(list2));
381+
Some(list1)
382+
} else {
383+
list2.next = Self::merge_two_lists(Some(list1), list2.next);
384+
Some(list2)
385+
}
386+
}
387+
}
388+
}
389+
}
390+
```
391+
392+
循环:
393+
394+
```rust
395+
// Definition for singly-linked list.
396+
// #[derive(PartialEq, Eq, Clone, Debug)]
397+
// pub struct ListNode {
398+
// pub val: i32,
399+
// pub next: Option<Box<ListNode>>
400+
// }
401+
//
402+
// impl ListNode {
403+
// #[inline]
404+
// fn new(val: i32) -> Self {
405+
// ListNode {
406+
// next: None,
407+
// val
408+
// }
409+
// }
410+
// }
411+
impl Solution {
412+
pub fn merge_two_lists(
413+
mut list1: Option<Box<ListNode>>,
414+
mut list2: Option<Box<ListNode>>,
415+
) -> Option<Box<ListNode>> {
416+
let mut new_list = ListNode::new(0);
417+
let mut cur = &mut new_list;
418+
while list1.is_some() && list2.is_some() {
419+
let (l1, l2) = (list1.as_deref_mut().unwrap(), list2.as_deref_mut().unwrap());
420+
if l1.val < l2.val {
421+
let next = l1.next.take();
422+
cur.next = list1.take();
423+
list1 = next;
424+
} else {
425+
let next = l2.next.take();
426+
cur.next = list2.take();
427+
list2 = next;
428+
}
429+
cur = cur.next.as_deref_mut().unwrap();
430+
}
431+
cur.next = list1.or(list2);
432+
new_list.next
433+
}
434+
}
435+
```
436+
278437
### **...**
279438

280439
```

solution/0000-0099/0021.Merge Two Sorted Lists/README_EN.md

+159
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,165 @@ public class Solution {
263263
}
264264
```
265265

266+
### **TypeScript**
267+
268+
Recursion:
269+
270+
```ts
271+
/**
272+
* Definition for singly-linked list.
273+
* class ListNode {
274+
* val: number
275+
* next: ListNode | null
276+
* constructor(val?: number, next?: ListNode | null) {
277+
* this.val = (val===undefined ? 0 : val)
278+
* this.next = (next===undefined ? null : next)
279+
* }
280+
* }
281+
*/
282+
283+
function mergeTwoLists(
284+
list1: ListNode | null,
285+
list2: ListNode | null,
286+
): ListNode | null {
287+
if (list1 == null || list2 == null) {
288+
return list1 || list2;
289+
}
290+
if (list1.val < list2.val) {
291+
list1.next = mergeTwoLists(list1.next, list2);
292+
return list1;
293+
} else {
294+
list2.next = mergeTwoLists(list1, list2.next);
295+
return list2;
296+
}
297+
}
298+
```
299+
300+
Loop:
301+
302+
```ts
303+
/**
304+
* Definition for singly-linked list.
305+
* class ListNode {
306+
* val: number
307+
* next: ListNode | null
308+
* constructor(val?: number, next?: ListNode | null) {
309+
* this.val = (val===undefined ? 0 : val)
310+
* this.next = (next===undefined ? null : next)
311+
* }
312+
* }
313+
*/
314+
315+
function mergeTwoLists(
316+
list1: ListNode | null,
317+
list2: ListNode | null,
318+
): ListNode | null {
319+
const dummy = new ListNode(0);
320+
let cur = dummy;
321+
while (list1 != null && list2 != null) {
322+
if (list1.val < list2.val) {
323+
cur.next = list1;
324+
list1 = list1.next;
325+
} else {
326+
cur.next = list2;
327+
list2 = list2.next;
328+
}
329+
cur = cur.next;
330+
}
331+
cur.next = list1 || list2;
332+
return dummy.next;
333+
}
334+
```
335+
336+
### **Rust**
337+
338+
Recursion:
339+
340+
```rust
341+
// Definition for singly-linked list.
342+
// #[derive(PartialEq, Eq, Clone, Debug)]
343+
// pub struct ListNode {
344+
// pub val: i32,
345+
// pub next: Option<Box<ListNode>>
346+
// }
347+
//
348+
// impl ListNode {
349+
// #[inline]
350+
// fn new(val: i32) -> Self {
351+
// ListNode {
352+
// next: None,
353+
// val
354+
// }
355+
// }
356+
// }
357+
impl Solution {
358+
pub fn merge_two_lists(
359+
list1: Option<Box<ListNode>>,
360+
list2: Option<Box<ListNode>>,
361+
) -> Option<Box<ListNode>> {
362+
match (list1, list2) {
363+
(None, None) => None,
364+
(Some(list), None) => Some(list),
365+
(None, Some(list)) => Some(list),
366+
(Some(mut list1), Some(mut list2)) => {
367+
if list1.val < list2.val {
368+
list1.next = Self::merge_two_lists(list1.next, Some(list2));
369+
Some(list1)
370+
} else {
371+
list2.next = Self::merge_two_lists(Some(list1), list2.next);
372+
Some(list2)
373+
}
374+
}
375+
}
376+
}
377+
}
378+
```
379+
380+
Loop:
381+
382+
```rust
383+
// Definition for singly-linked list.
384+
// #[derive(PartialEq, Eq, Clone, Debug)]
385+
// pub struct ListNode {
386+
// pub val: i32,
387+
// pub next: Option<Box<ListNode>>
388+
// }
389+
//
390+
// impl ListNode {
391+
// #[inline]
392+
// fn new(val: i32) -> Self {
393+
// ListNode {
394+
// next: None,
395+
// val
396+
// }
397+
// }
398+
// }
399+
impl Solution {
400+
pub fn merge_two_lists(
401+
mut list1: Option<Box<ListNode>>,
402+
mut list2: Option<Box<ListNode>>,
403+
) -> Option<Box<ListNode>> {
404+
let mut new_list = ListNode::new(0);
405+
let mut cur = &mut new_list;
406+
while list1.is_some() && list2.is_some() {
407+
let (l1, l2) = (list1.as_deref_mut().unwrap(), list2.as_deref_mut().unwrap());
408+
if l1.val < l2.val {
409+
let next = l1.next.take();
410+
cur.next = list1.take();
411+
list1 = next;
412+
} else {
413+
let next = l2.next.take();
414+
cur.next = list2.take();
415+
list2 = next;
416+
}
417+
cur = cur.next.as_deref_mut().unwrap();
418+
}
419+
cur.next = list1.or(list2);
420+
new_list.next
421+
}
422+
}
423+
```
424+
266425
### **...**
267426

268427
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Definition for singly-linked list.
2+
// #[derive(PartialEq, Eq, Clone, Debug)]
3+
// pub struct ListNode {
4+
// pub val: i32,
5+
// pub next: Option<Box<ListNode>>
6+
// }
7+
//
8+
// impl ListNode {
9+
// #[inline]
10+
// fn new(val: i32) -> Self {
11+
// ListNode {
12+
// next: None,
13+
// val
14+
// }
15+
// }
16+
// }
17+
impl Solution {
18+
pub fn merge_two_lists(
19+
list1: Option<Box<ListNode>>,
20+
list2: Option<Box<ListNode>>,
21+
) -> Option<Box<ListNode>> {
22+
match (list1, list2) {
23+
(None, None) => None,
24+
(Some(list), None) => Some(list),
25+
(None, Some(list)) => Some(list),
26+
(Some(mut list1), Some(mut list2)) => {
27+
if list1.val < list2.val {
28+
list1.next = Self::merge_two_lists(list1.next, Some(list2));
29+
Some(list1)
30+
} else {
31+
list2.next = Self::merge_two_lists(Some(list1), list2.next);
32+
Some(list2)
33+
}
34+
}
35+
}
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* class ListNode {
4+
* val: number
5+
* next: ListNode | null
6+
* constructor(val?: number, next?: ListNode | null) {
7+
* this.val = (val===undefined ? 0 : val)
8+
* this.next = (next===undefined ? null : next)
9+
* }
10+
* }
11+
*/
12+
13+
function mergeTwoLists(
14+
list1: ListNode | null,
15+
list2: ListNode | null,
16+
): ListNode | null {
17+
if (list1 == null || list2 == null) {
18+
return list1 || list2;
19+
}
20+
if (list1.val < list2.val) {
21+
list1.next = mergeTwoLists(list1.next, list2);
22+
return list1;
23+
} else {
24+
list2.next = mergeTwoLists(list1, list2.next);
25+
return list2;
26+
}
27+
}

0 commit comments

Comments
 (0)