3217. 从链表中移除在数组中存在的节点 ps: Rust写数据结构还是太权威了
This commit is contained in:
+33
@@ -0,0 +1,33 @@
|
||||
// Definition for singly-linked list.
|
||||
#[derive(PartialEq, Eq, Clone, Debug)]
|
||||
pub struct ListNode {
|
||||
pub val: i32,
|
||||
pub next: Option<Box<ListNode>>
|
||||
}
|
||||
|
||||
impl ListNode {
|
||||
#[inline]
|
||||
fn new(val: i32) -> Self {
|
||||
ListNode {
|
||||
next: None,
|
||||
val
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn make_list(arr: Vec<i32>) -> Option<Box<ListNode>> {
|
||||
let mut head: Option<Box<ListNode>> = None;
|
||||
let mut current = &mut head;
|
||||
|
||||
for &value in arr.iter() {
|
||||
let new_node = Box::new(ListNode::new(value));
|
||||
if current.is_none() {
|
||||
*current = Some(new_node);
|
||||
} else {
|
||||
current.as_mut().unwrap().next = Some(new_node);
|
||||
current = &mut current.as_mut().unwrap().next;
|
||||
}
|
||||
}
|
||||
|
||||
head
|
||||
}
|
||||
Reference in New Issue
Block a user