-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathadd-two-numbers-ii.rs
81 lines (76 loc) · 2.19 KB
/
add-two-numbers-ii.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// 445. Add Two Numbers II
// 🟠 Medium
//
// https://leetcode.com/problems/add-two-numbers-ii/
//
// Tags: Linked List - Math - Stack
// 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 }
}
}
struct Solution;
impl Solution {
/// Use the solution to problems that have been solved previously as the
/// base for solving this one, reverse the input lists and add them, then
/// return the result in reverse.
///
/// Time complexity: O(n) - We visit each node.
/// Space complexity: O(1) - We use constant extra space.
///
/// Runtime 3 ms Beats 87.50%
/// Memory 2.21 MB Beats 50.00%
pub fn add_two_numbers(
l1: Option<Box<ListNode>>,
l2: Option<Box<ListNode>>,
) -> Option<Box<ListNode>> {
Self::reverse_list(Self::add_two_helper(
Self::reverse_list(l1),
Self::reverse_list(l2),
))
}
pub fn reverse_list(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
let (mut prev, mut curr) = (None, head);
while let Some(mut node) = curr {
curr = node.next;
node.next = prev;
prev = Some(node);
}
prev
}
pub fn add_two_helper(
l1: Option<Box<ListNode>>,
l2: Option<Box<ListNode>>,
) -> Option<Box<ListNode>> {
let mut dummy = Box::new(ListNode::new(-1));
let mut curr = &mut dummy;
let (mut a, mut b) = (&l1, &l2);
let mut carry = 0;
// let mut val = 0;
while *a != None || *b != None || carry != 0 {
if let Some(node) = a {
carry += node.val;
a = &node.next;
}
if let Some(node) = b {
carry += node.val;
b = &node.next;
}
curr.next = Some(Box::new(ListNode::new(carry % 10)));
curr = curr.next.as_mut().unwrap();
carry /= 10;
}
dummy.next
}
}
// Tests.
fn main() {
println!("[92m» All tests passed![0m")
}