mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
update [19] 删除链表的倒数第 N 个结点 about rust
This commit is contained in:
@ -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>
|
||||
|
Reference in New Issue
Block a user