From 250dd8b8f3dc0fbf5925588eac09ed352b5616cc Mon Sep 17 00:00:00 2001 From: a1045520 Date: Tue, 2 Nov 2021 15:35:16 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=200203.=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E9=93=BE=E8=A1=A8=E5=85=83=E7=B4=A0=20Rust=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0203.移除链表元素.md | 35 +++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) 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)