mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 08:50:15 +08:00
增加 0203.移除链表元素 Rust版本
This commit is contained in:
@ -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<Box<ListNode>>
|
||||
// }
|
||||
//
|
||||
// impl ListNode {
|
||||
// #[inline]
|
||||
// fn new(val: i32) -> Self {
|
||||
// ListNode {
|
||||
// next: None,
|
||||
// val
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
impl Solution {
|
||||
pub fn remove_elements(head: Option<Box<ListNode>>, val: i32) -> Option<Box<ListNode>> {
|
||||
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)
|
||||
|
Reference in New Issue
Block a user