Skip to content

Commit 3f7932f

Browse files
committedMay 22, 2022
feat: update solutions to lcof problem: No.25
面试题25. 合并两个排序的链表
1 parent 2d78869 commit 3f7932f

File tree

3 files changed

+88
-10
lines changed

3 files changed

+88
-10
lines changed
 

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

+83-5
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,8 @@ function mergeTwoLists(
209209
l1: ListNode | null,
210210
l2: ListNode | null,
211211
): ListNode | null {
212-
const res = new ListNode();
213-
let cur = res;
212+
const duumy = new ListNode();
213+
let cur = duumy;
214214
while (l1 && l2) {
215215
let node: ListNode;
216216
if (l1.val < l2.val) {
@@ -224,7 +224,36 @@ function mergeTwoLists(
224224
cur = node;
225225
}
226226
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;
228257
}
229258
```
230259

@@ -255,10 +284,10 @@ impl Solution {
255284
match (l1, l2) {
256285
(Some(mut n1), Some(mut n2)) => {
257286
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));
259288
Some(n1)
260289
} else {
261-
n2.next = Solution::merge_two_lists(Some(n1), n2.next);
290+
n2.next = Self::merge_two_lists(Some(n1), n2.next);
262291
Some(n2)
263292
}
264293
}
@@ -270,6 +299,55 @@ impl Solution {
270299
}
271300
```
272301

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+
273351
### **...**
274352

275353
```

‎lcof/面试题25. 合并两个排序的链表/Solution.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ impl Solution {
2222
match (l1, l2) {
2323
(Some(mut n1), Some(mut n2)) => {
2424
if n1.val < n2.val {
25-
n1.next = Solution::merge_two_lists(n1.next, Some(n2));
25+
n1.next = Self::merge_two_lists(n1.next, Some(n2));
2626
Some(n1)
2727
} else {
28-
n2.next = Solution::merge_two_lists(Some(n1), n2.next);
28+
n2.next = Self::merge_two_lists(Some(n1), n2.next);
2929
Some(n2)
3030
}
3131
}

‎lcof/面试题25. 合并两个排序的链表/Solution.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ function mergeTwoLists(
1414
l1: ListNode | null,
1515
l2: ListNode | null,
1616
): ListNode | null {
17-
const res = new ListNode();
18-
let cur = res;
17+
const duumy = new ListNode();
18+
let cur = duumy;
1919
while (l1 && l2) {
2020
let node: ListNode;
2121
if (l1.val < l2.val) {
@@ -29,5 +29,5 @@ function mergeTwoLists(
2929
cur = node;
3030
}
3131
cur.next = l1 || l2;
32-
return res.next;
32+
return duumy.next;
3333
}

0 commit comments

Comments
 (0)
Please sign in to comment.