3217. 从链表中移除在数组中存在的节点 ps: Rust写数据结构还是太权威了

This commit is contained in:
li-chx
2025-11-04 21:13:02 +08:00
parent 941d05d9ee
commit fc7f57b9ca
2 changed files with 74 additions and 90 deletions
+33
View File
@@ -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
}