From c58be464b4ca65d6d396c744dafbc3df984bf206 Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Sat, 1 Oct 2022 16:21:39 +0800 Subject: [PATCH] =?UTF-8?q?update=20[19]=20=E5=88=A0=E9=99=A4=E9=93=BE?= =?UTF-8?q?=E8=A1=A8=E7=9A=84=E5=80=92=E6=95=B0=E7=AC=AC=20N=20=E4=B8=AA?= =?UTF-8?q?=E7=BB=93=E7=82=B9=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0019.删除链表的倒数第N个节点.md | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) 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 + } +} +``` + -----------------------