Skip to content

Commit d988919

Browse files
committed
Create: 0021-merge-two-sorted-lists.rs
1 parent 9154cf7 commit d988919

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

Diff for: rust/0021-merge-two-sorted-lists.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
impl Solution {
2+
pub fn merge_two_lists(list1: Option<Box<ListNode>>, list2: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
3+
match (list1,list2) {
4+
(Some(list1),None)=> Some(list1),
5+
(None,Some(list2))=>Some(list2),
6+
(None,None)=>None,
7+
(Some(l1),Some(l2))=>{
8+
if l1.val<l2.val {
9+
return Some(Box::new(ListNode{val:l1.val,next:Solution::merge_two_lists(l1.next,Some(l2))}));
10+
}else {
11+
return Some(Box::new(ListNode { val: l2.val, next: Solution::merge_two_lists(Some(l1),l2.next) }))
12+
}
13+
}
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)