Skip to content

Commit d26077c

Browse files
author
Zhang Xiaodong
committed
solve 21
1 parent 5f7467a commit d26077c

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

src/solution/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ mod s0017_letter_combinations_of_a_phone_number;
1616
mod s0018_4sum;
1717
mod s0019_remove_nth_node_from_end_of_list;
1818
mod s0020_valid_parentheses;
19+
mod s0021_merge_two_sorted_lists;
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/**
2+
* [21] 合并两个有序链表
3+
*
4+
* 将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 
5+
*  
6+
* 示例 1:
7+
* <img alt="" src="https://assets.leetcode.com/uploads/2020/10/03/merge_ex1.jpg" style="width: 662px; height: 302px;" />
8+
* 输入:l1 = [1,2,4], l2 = [1,3,4]
9+
* 输出:[1,1,2,3,4,4]
10+
*
11+
* 示例 2:
12+
*
13+
* 输入:l1 = [], l2 = []
14+
* 输出:[]
15+
*
16+
* 示例 3:
17+
*
18+
* 输入:l1 = [], l2 = [0]
19+
* 输出:[0]
20+
*
21+
*  
22+
* 提示:
23+
*
24+
* 两个链表的节点数目范围是 [0, 50]
25+
* -100 <= Node.val <= 100
26+
* l1 和 l2 均按 非递减顺序 排列
27+
*
28+
*/
29+
pub struct Solution {}
30+
use crate::util::linked_list::{ListNode, to_list};
31+
32+
// problem: https://leetcode.cn/problems/merge-two-sorted-lists/
33+
// discuss: https://leetcode.cn/problems/merge-two-sorted-lists/discuss/?currentPage=1&orderBy=most_votes&query=
34+
35+
// submission codes start here
36+
37+
// Definition for singly-linked list.
38+
// #[derive(PartialEq, Eq, Clone, Debug)]
39+
// pub struct ListNode {
40+
// pub val: i32,
41+
// pub next: Option<Box<ListNode>>
42+
// }
43+
//
44+
// impl ListNode {
45+
// #[inline]
46+
// fn new(val: i32) -> Self {
47+
// ListNode {
48+
// next: None,
49+
// val
50+
// }
51+
// }
52+
// }
53+
impl Solution {
54+
pub fn merge_two_lists(list1: Option<Box<ListNode>>, list2: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
55+
let mut dummy_head = Some(Box::new(ListNode {
56+
val: 0,
57+
next: None,
58+
}));
59+
let mut p = &mut dummy_head;
60+
let mut p1 = list1;
61+
let mut p2 = list2;
62+
while p1.is_some() || p2.is_some() {
63+
if p1.is_none() {
64+
p.as_mut().unwrap().next = p2;
65+
break;
66+
} else if p2.is_none() {
67+
p.as_mut().unwrap().next = p1;
68+
break;
69+
}
70+
let next = if p1.as_ref().unwrap().val > p2.as_ref().unwrap().val {
71+
let n = p2.as_mut().unwrap().next.take();
72+
let next = p2.take();
73+
p2 = n;
74+
next
75+
} else {
76+
let n = p1.as_mut().unwrap().next.take();
77+
let next = p1.take();
78+
p1 = n;
79+
next
80+
};
81+
p.as_mut().unwrap().next = next;
82+
p = &mut p.as_mut().unwrap().next;
83+
}
84+
dummy_head.unwrap().next
85+
}
86+
}
87+
88+
// submission codes end
89+
90+
#[cfg(test)]
91+
mod tests {
92+
use super::*;
93+
94+
#[test]
95+
fn test_21() {
96+
assert_eq!(
97+
Solution::merge_two_lists(to_list(vec![1, 2, 4]), to_list(vec![1, 3, 4])),
98+
to_list(vec![1, 1, 2, 3, 4, 4])
99+
);
100+
}
101+
}

0 commit comments

Comments
 (0)