Skip to content

Instantly share code, notes, and snippets.

@dimitrilw
Last active March 15, 2024 23:24
Show Gist options
  • Save dimitrilw/12d00c0ea8625fe1e4ca672d2366ddb2 to your computer and use it in GitHub Desktop.
Save dimitrilw/12d00c0ea8625fe1e4ca672d2366ddb2 to your computer and use it in GitHub Desktop.
convert vector of i32s into a LeetCode linked list
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