-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd-two-numbers.rs
More file actions
77 lines (73 loc) · 2.21 KB
/
add-two-numbers.rs
File metadata and controls
77 lines (73 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Definition for singly-linked list.
// #[derive(PartialEq, Eq, Clone, Debug)]
// pub struct ListNode {
// pub val: i32,
// pub next: Option<Box<ListNode>>
// }
//
// impl ListNode {
// #[inline]
// fn new(val: i32) -> Self {
// ListNode {
// next: None,
// val
// }
// }
// }
impl Solution {
pub fn add_two_numbers(
l1: Option<Box<ListNode>>,
l2: Option<Box<ListNode>>
) -> Option<Box<ListNode>> {
let mut r = 0;
let mut x_pos = l1.as_ref();
let mut y_pos = l2.as_ref();
let mut result = Some(Box::new(ListNode::new(0)));
let mut pos = result.as_mut();
loop {
match (x_pos, y_pos) {
(None, None) => {
if r >= 10 {
Solution::add_node(
r / 10,
pos.unwrap()
);
}
break;
},
(Some(x), Some(y)) => {
let lsum = x.val + y.val + r / 10;
pos = Some(Solution::add_node(
lsum % 10,
pos.unwrap()
));
r = lsum;
},
(Some(x), None) => {
let lsum = x.val + r / 10;
pos = Some(Solution::add_node(
lsum % 10,
pos.unwrap()
));
r = lsum;
},
(None, Some(y)) => {
let lsum = y.val + r / 10;
pos = Some(Solution::add_node(
lsum % 10,
pos.unwrap()
));
r = lsum;
}
}
if x_pos.is_some() { x_pos = x_pos.unwrap().next.as_ref() };
if y_pos.is_some() { y_pos = y_pos.unwrap().next.as_ref() };
}
result.unwrap().next
}
pub fn add_node(val: i32, pos: &mut Box<ListNode>) -> &mut Box<ListNode> {
let new_node: Box<ListNode> = Box::new(ListNode::new(val));
pos.next = Some(new_node);
pos.next.as_mut().unwrap()
}
}