Last active
March 15, 2024 23:24
-
-
Save dimitrilw/12d00c0ea8625fe1e4ca672d2366ddb2 to your computer and use it in GitHub Desktop.
convert vector of i32s into a LeetCode linked list
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
impl Solution { | |
// ...stuff... | |
// Returns a LeetCode linked list from the given vec. | |
fn vec_to_list(vec: Vec<i32>) -> Option<Box<ListNode>> { | |
let mut root: Option<Box<ListNode>> = None; | |
let mut node = &mut root; | |
for v in vec { | |
node.replace(Box::new(ListNode::new(v))); | |
if let Some(n) = node { | |
node = &mut n.next; | |
} | |
} | |
root | |
} | |
// ...more stuff... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment