diff --git a/problems/0203.移除链表元素.md b/problems/0203.移除链表元素.md index 6c52886a..188edc2e 100644 --- a/problems/0203.移除链表元素.md +++ b/problems/0203.移除链表元素.md @@ -358,6 +358,41 @@ func removeElements(head *ListNode, val int) *ListNode { } ``` +RUST: +```rust +// Definition for singly-linked list. +// #[derive(PartialEq, Eq, Clone, Debug)] +// pub struct ListNode { +// pub val: i32, +// pub next: Option> +// } +// +// impl ListNode { +// #[inline] +// fn new(val: i32) -> Self { +// ListNode { +// next: None, +// val +// } +// } +// } +impl Solution { + pub fn remove_elements(head: Option>, val: i32) -> Option> { + let mut head = head; + let mut dummy_head = ListNode::new(0); + let mut cur = &mut dummy_head; + while let Some(mut node) = head { + head = std::mem::replace(&mut node.next, None); + if node.val != val { + cur.next = Some(node); + cur = cur.next.as_mut().unwrap(); + } + } + dummy_head.next + } +} +``` + ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321)