forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution2.rs
53 lines (51 loc) · 1.31 KB
/
Solution2.rs
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
// 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 {
fn create_stack(mut head: Option<Box<ListNode>>) -> Vec<i32> {
let mut res = vec![];
while let Some(node) = head {
res.push(node.val);
head = node.next;
}
res
}
pub fn add_two_numbers(
l1: Option<Box<ListNode>>,
l2: Option<Box<ListNode>>
) -> Option<Box<ListNode>> {
let mut s1 = Self::create_stack(l1);
let mut s2 = Self::create_stack(l2);
let mut dummy = Box::new(ListNode::new(0));
let mut carry = 0;
while !s1.is_empty() || !s2.is_empty() || carry != 0 {
if let Some(val) = s1.pop() {
carry += val;
}
if let Some(val) = s2.pop() {
carry += val;
}
dummy.next = Some(
Box::new(ListNode {
val: carry % 10,
next: dummy.next.take(),
})
);
carry /= 10;
}
dummy.next.take()
}
}