forked from krahets/hello-algo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix rust compile warning and an obvious print error in array.rs (krah…
…ets#1144) * Fix rust compile warning and an obvious print error in array.rs * Update LinkedList 1. drop unnessaray mut borrow 2. fmt code and make variable more readable * follow convention of this repo * Update list_node.rs --------- Co-authored-by: Yudong Jin <[email protected]>
- Loading branch information
Showing
4 changed files
with
15 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
/* | ||
* File: list_node.rs | ||
* Created Time: 2023-03-05 | ||
* Author: codingonion ([email protected]) | ||
* Author: codingonion ([email protected]), rongyi ([email protected]) | ||
*/ | ||
|
||
use std::rc::Rc; | ||
use std::cell::RefCell; | ||
use std::collections::HashMap; | ||
use std::rc::Rc; | ||
|
||
#[derive(Debug)] | ||
pub struct ListNode<T> { | ||
|
@@ -16,25 +16,21 @@ pub struct ListNode<T> { | |
|
||
impl<T> ListNode<T> { | ||
pub fn new(val: T) -> Rc<RefCell<ListNode<T>>> { | ||
Rc::new(RefCell::new(ListNode { | ||
val, | ||
next: None, | ||
})) | ||
Rc::new(RefCell::new(ListNode { val, next: None })) | ||
} | ||
|
||
/* Generate a linked list with an array */ | ||
pub fn arr_to_linked_list(array: &[T]) -> Option<Rc<RefCell<ListNode<T>>>> | ||
pub fn arr_to_linked_list(array: &[T]) -> Option<Rc<RefCell<ListNode<T>>>> | ||
where | ||
T: Copy + Clone, | ||
{ | ||
let mut head = None; | ||
let mut prev = None; | ||
// insert in reverse order | ||
for item in array.iter().rev() { | ||
let node = Rc::new(RefCell::new(ListNode { | ||
val: *item, | ||
next: prev.take(), | ||
next: head.clone(), | ||
})); | ||
prev = Some(node.clone()); | ||
head = Some(node); | ||
} | ||
head | ||
|
@@ -43,9 +39,9 @@ impl<T> ListNode<T> { | |
/* Generate a hashmap with a linked_list */ | ||
pub fn linked_list_to_hashmap( | ||
linked_list: Option<Rc<RefCell<ListNode<T>>>>, | ||
) -> HashMap<T, Rc<RefCell<ListNode<T>>>> | ||
) -> HashMap<T, Rc<RefCell<ListNode<T>>>> | ||
where | ||
T: std::hash::Hash + Eq + Copy + Clone | ||
T: std::hash::Hash + Eq + Copy + Clone, | ||
{ | ||
let mut hashmap = HashMap::new(); | ||
if let Some(node) = linked_list { | ||
|