Merge pull request #1677 from Jack-Zhang-1314/patch-7

update [19] 删除链表的倒数第 N 个结点 about rust
This commit is contained in:
程序员Carl
2022-10-08 09:48:19 +08:00
committed by GitHub

View File

@ -338,5 +338,28 @@ object Solution {
} }
} }
``` ```
Rust:
```rust
impl Solution {
pub fn remove_nth_from_end(head: Option<Box<ListNode>>, mut n: i32) -> Option<Box<ListNode>> {
let mut dummy_head = Box::new(ListNode::new(0));
dummy_head.next = head;
let mut fast = &dummy_head.clone();
let mut slow = &mut dummy_head;
while n > 0 {
fast = fast.next.as_ref().unwrap();
n -= 1;
}
while fast.next.is_some() {
fast = fast.next.as_ref().unwrap();
slow = slow.next.as_mut().unwrap();
}
slow.next = slow.next.as_mut().unwrap().next.take();
dummy_head.next
}
}
```
----------------------- -----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div> <div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>