diff --git a/problems/0019.删除链表的倒数第N个节点.md b/problems/0019.删除链表的倒数第N个节点.md index a84c0b29..b9859968 100644 --- a/problems/0019.删除链表的倒数第N个节点.md +++ b/problems/0019.删除链表的倒数第N个节点.md @@ -338,5 +338,28 @@ object Solution { } } ``` + +Rust: +```rust +impl Solution { + pub fn remove_nth_from_end(head: Option>, mut n: i32) -> Option> { + 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 + } +} +``` + -----------------------