Skip to content

Commit 30468b0

Browse files
committed
0002
1 parent 58f0ea2 commit 30468b0

File tree

3 files changed

+33
-96
lines changed

3 files changed

+33
-96
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Definition for singly-linked list.
2+
#[derive(PartialEq, Eq, Debug)]
3+
pub struct ListNode {
4+
pub val: i32,
5+
pub next: Option<Box<ListNode>>
6+
}
7+
8+
impl ListNode {
9+
#[inline]
10+
fn new(val: i32) -> Self {
11+
ListNode {
12+
next: None,
13+
val
14+
}
15+
}
16+
}

Algorithms/0002.add_two_numbers/add_two_numbers.rs

Lines changed: 15 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -2,108 +2,28 @@
22
use std::collections::LinkedList;
33
use std::cmp;
44

5-
// Definition for singly-linked list.
6-
// #[derive(PartialEq, Eq, 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 add_two_numbers(l1: Option<Box<ListNode>>, l2: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
24-
// let mut results: LinkedList<u32> = LinkedList::new();
25-
26-
// let length = cmp::max(list1.len(), list2.len()) + 1;
27-
// let mut iter1 = list1.iter();
28-
// let mut iter2 = list2.iter();
29-
30-
// let mut overflow_value: u32 = 0;
31-
32-
// for _i in 0..length {
33-
// let i1 = iter1.next();
34-
// let i2 = iter2.next();
35-
36-
// let mut v1 = 0;
37-
// let mut v2 = 0;
38-
39-
// if i1.is_some() {
40-
// v1 = *i1.unwrap();
41-
// }
42-
// if i2.is_some() {
43-
// v2 = *i2.unwrap();
44-
// }
45-
46-
// let mut v = v1 + v2 + overflow_value;
47-
48-
// overflow_value = v / 10;
49-
50-
// v = v % 10;
51-
52-
// results.push_back(v);
53-
// }
54-
55-
// let last_value = results.pop_back();
56-
// if last_value.is_some() && last_value.unwrap() != 0 {
57-
// results.push_back(last_value.unwrap());
58-
// }
59-
60-
// return results;
61-
// }
62-
// }
635
impl Solution {
64-
pub fn add_two_numbers(l1: Option<Box<ListNode>>, l2: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
65-
let mut one = l1.unwrap();
66-
let mut two = l2.unwrap();
67-
let mut root = ListNode::new(0);
68-
69-
let mut res = Solution::make_node(one.val + two.val);
70-
root.next.get_or_insert(Box::new(res.0));
71-
72-
let mut curr = &mut root.next;
6+
pub fn add_two_numbers(l1: Option<Box<ListNode>>, l2: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
7+
let mut results: ListNode::new(0);
8+
let mut i1 = l1;
9+
let mut i2 = l2;
7310

74-
while one.next.is_some() || two.next.is_some() {
75-
match curr {
76-
None => break,
77-
Some(now) => {
78-
one = one.next.or(Some(Box::new(ListNode::new(0)))).unwrap();
79-
two = two.next.or(Some(Box::new(ListNode::new(0)))).unwrap();
11+
let mut overfalow_value: i32 = 0;
8012

81-
res = Solution::make_node(one.val + two.val + res.1);
13+
while i1.is_some() || i2.is_some() {
14+
let mut v1 = i1.unwrap_or(0);
15+
let mut v2 = i2.unwrap_or(0);
8216

83-
now.next.get_or_insert(Box::new(res.0));
84-
curr = &mut now.next;
85-
}
86-
}
87-
}
88-
89-
if res.1 > 0 {
90-
if let Some(now) = curr {
91-
now.next.get_or_insert(Box::new(ListNode::new(res.1)));
92-
}
93-
}
17+
let mut v = v1 + v2 + overflow_value;
18+
overflow_value = v / 10;
19+
v = v % 10;
9420

95-
root.next
96-
}
21+
results.next.push_back(Some(Box::new(ListNode::new(v))));
9722

98-
fn make_node(mut result: i32) -> (ListNode, i32) {
99-
let single;
100-
if result > 9 {
101-
single = 1;
102-
result = result - 10;
103-
} else {
104-
single = 0;
23+
i1 = iter1.next;
24+
i2 = iter2.next;
10525
}
106-
(ListNode::new(result), single)
26+
return results;
10727
}
10828
}
10929

Algorithms/0002.add_two_numbers/add_two_numbers_test.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
mod add_two_numbers;
2+
mod ListNode;
23
use add_two_numbers::Solution;
34

45
fn main() {
@@ -14,6 +15,6 @@ fn main() {
1415
list2.push_back(5);
1516
list2.push_back(5);
1617

17-
let results = add_two_numbers(list1, list2);
18+
let results = Solution.add_two_numbers(list1, list2);
1819
println!("{:?}", results);
1920
}

0 commit comments

Comments
 (0)