Skip to content

Commit ced8022

Browse files
committed
feat: add solutions to lcof problem: No.25
面试题25. 合并两个排序的链表
1 parent 8102809 commit ced8022

File tree

3 files changed

+150
-0
lines changed

3 files changed

+150
-0
lines changed

lcof/面试题25. 合并两个排序的链表/README.md

+80
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,86 @@ public:
176176
};
177177
```
178178

179+
### **TypeScript**
180+
181+
```ts
182+
/**
183+
* Definition for singly-linked list.
184+
* class ListNode {
185+
* val: number
186+
* next: ListNode | null
187+
* constructor(val?: number, next?: ListNode | null) {
188+
* this.val = (val===undefined ? 0 : val)
189+
* this.next = (next===undefined ? null : next)
190+
* }
191+
* }
192+
*/
193+
194+
function mergeTwoLists(
195+
l1: ListNode | null,
196+
l2: ListNode | null
197+
): ListNode | null {
198+
const res = new ListNode();
199+
let cur = res;
200+
while (l1 && l2) {
201+
let node: ListNode;
202+
if (l1.val < l2.val) {
203+
node = l1;
204+
l1 = l1.next;
205+
} else {
206+
node = l2;
207+
l2 = l2.next;
208+
}
209+
cur.next = node;
210+
cur = node;
211+
}
212+
cur.next = l1 || l2;
213+
return res.next;
214+
}
215+
```
216+
217+
### **Rust**
218+
219+
```rust
220+
// Definition for singly-linked list.
221+
// #[derive(PartialEq, Eq, Clone, Debug)]
222+
// pub struct ListNode {
223+
// pub val: i32,
224+
// pub next: Option<Box<ListNode>>
225+
// }
226+
//
227+
// impl ListNode {
228+
// #[inline]
229+
// fn new(val: i32) -> Self {
230+
// ListNode {
231+
// next: None,
232+
// val
233+
// }
234+
// }
235+
// }
236+
impl Solution {
237+
pub fn merge_two_lists(
238+
l1: Option<Box<ListNode>>,
239+
l2: Option<Box<ListNode>>,
240+
) -> Option<Box<ListNode>> {
241+
match (l1, l2) {
242+
(Some(mut n1), Some(mut n2)) => {
243+
if n1.val < n2.val {
244+
n1.next = Solution::merge_two_lists(n1.next, Some(n2));
245+
Some(n1)
246+
} else {
247+
n2.next = Solution::merge_two_lists(Some(n1), n2.next);
248+
Some(n2)
249+
}
250+
}
251+
(Some(node), None) => Some(node),
252+
(None, Some(node)) => Some(node),
253+
(None, None) => None,
254+
}
255+
}
256+
}
257+
```
258+
179259
### **...**
180260

181261
```
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+
l1: Option<Box<ListNode>>,
20+
l2: Option<Box<ListNode>>,
21+
) -> Option<Box<ListNode>> {
22+
match (l1, l2) {
23+
(Some(mut n1), Some(mut n2)) => {
24+
if n1.val < n2.val {
25+
n1.next = Solution::merge_two_lists(n1.next, Some(n2));
26+
Some(n1)
27+
} else {
28+
n2.next = Solution::merge_two_lists(Some(n1), n2.next);
29+
Some(n2)
30+
}
31+
}
32+
(Some(node), None) => Some(node),
33+
(None, Some(node)) => Some(node),
34+
(None, None) => None,
35+
}
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
l1: ListNode | null,
15+
l2: ListNode | null
16+
): ListNode | null {
17+
const res = new ListNode();
18+
let cur = res;
19+
while (l1 && l2) {
20+
let node: ListNode;
21+
if (l1.val < l2.val) {
22+
node = l1;
23+
l1 = l1.next;
24+
} else {
25+
node = l2;
26+
l2 = l2.next;
27+
}
28+
cur.next = node;
29+
cur = node;
30+
}
31+
cur.next = l1 || l2;
32+
return res.next;
33+
}

0 commit comments

Comments
 (0)