Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 3d0a1c7

Browse files
committed
src/bin/merge-two-sorted-lists.rs
1 parent 626716f commit 3d0a1c7

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

src/bin/merge-two-sorted-lists.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
fn main() {}
2+
3+
struct Solution;
4+
5+
// Definition for singly-linked list.
6+
#[derive(PartialEq, Eq, Clone, Debug)]
7+
pub struct ListNode {
8+
pub val: i32,
9+
pub next: Option<Box<ListNode>>,
10+
}
11+
12+
impl ListNode {
13+
#[inline]
14+
fn new(val: i32) -> Self {
15+
ListNode {
16+
next: None,
17+
val,
18+
}
19+
}
20+
}
21+
22+
impl Solution {
23+
pub fn merge_two_lists(l1: Option<Box<ListNode>>, l2: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
24+
match (l1, l2) {
25+
(Some(mut x), Some(mut y)) => {
26+
if x.val <= y.val {
27+
x.next = Self::merge_two_lists(x.next, Some(y));
28+
Some(x)
29+
} else {
30+
y.next = Self::merge_two_lists(Some(x), y.next);
31+
Some(y)
32+
}
33+
}
34+
(None, Some(y)) => Some(y),
35+
(Some(x), None) => Some(x),
36+
(None, None) => None,
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)