diff options
-rw-r--r-- | works/solutions/.gitignore | 2 | ||||
-rw-r--r-- | works/solutions/src/add_two_numbers.rs | 24 |
2 files changed, 14 insertions, 12 deletions
diff --git a/works/solutions/.gitignore b/works/solutions/.gitignore index 6936990..f03942d 100644 --- a/works/solutions/.gitignore +++ b/works/solutions/.gitignore @@ -1,3 +1,5 @@ /target **/*.rs.bk Cargo.lock + +.vscode diff --git a/works/solutions/src/add_two_numbers.rs b/works/solutions/src/add_two_numbers.rs index 3e3122a..d2ca858 100644 --- a/works/solutions/src/add_two_numbers.rs +++ b/works/solutions/src/add_two_numbers.rs @@ -20,30 +20,29 @@ use std::ptr::NonNull; struct List {
head: Option<Box<ListNode>>,
- tail: NonNull<Option<Box<ListNode>>>,
+ tail: Option<NonNull<Box<ListNode>>>,
}
impl List {
fn new() -> List {
- let mut result = List {
+ List {
head: None,
- tail: NonNull::dangling(),
- };
- result.tail = NonNull::from(&mut result.head);
- return result;
+ tail: None,
+ }
}
fn append(&mut self, val: i32) {
let node = Some(Box::new(ListNode::new(val)));
unsafe {
- match self.tail.as_mut() {
+ match self.tail {
None => {
self.head = node;
- self.tail = NonNull::from(&mut self.head);
+ self.tail = Some(NonNull::from(self.head.as_mut().unwrap()));
}
- Some(t) => {
- t.next = node;
- self.tail = NonNull::from(&mut t.next);
+ Some(mut t) => {
+ let last_node = t.as_mut();
+ last_node.next = node;
+ self.tail = Some(NonNull::from(last_node.next.as_mut().unwrap()));
}
}
}
@@ -149,7 +148,8 @@ mod test { #[test]
fn test() {
assert_eq!(
- Solution::add_two_numbers(vec![2, 4, 3].into_list(), vec![5, 6, 4].into_list()).into_vec(),
+ Solution::add_two_numbers(vec![2, 4, 3].into_list(), vec![5, 6, 4].into_list())
+ .into_vec(),
vec![7, 0, 8]
);
assert_eq!(
|