Skip to content

Commit 28889fd

Browse files
committed
feat: add rust solution to lc problem: No.0019
No.0019.Remove Nth Node From End of List
1 parent ec80cd3 commit 28889fd

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

solution/0000-0099/0019.Remove Nth Node From End of List/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,43 @@ def remove_nth_from_end(head, n)
226226
end
227227
```
228228

229+
### **Rust**
230+
231+
```rust
232+
// Definition for singly-linked list.
233+
// #[derive(PartialEq, Eq, Clone, Debug)]
234+
// pub struct ListNode {
235+
// pub val: i32,
236+
// pub next: Option<Box<ListNode>>
237+
// }
238+
//
239+
// impl ListNode {
240+
// #[inline]
241+
// fn new(val: i32) -> Self {
242+
// ListNode {
243+
// next: None,
244+
// val
245+
// }
246+
// }
247+
// }
248+
impl Solution {
249+
pub fn remove_nth_from_end(head: Option<Box<ListNode>>, n: i32) -> Option<Box<ListNode>> {
250+
let mut dummy = Some(Box::new(ListNode { val: 0, next: head }));
251+
let mut slow = &mut dummy;
252+
let mut fast = &slow.clone();
253+
for _ in 0..=n {
254+
fast = &fast.as_ref().unwrap().next;
255+
}
256+
while fast.is_some() {
257+
fast = &fast.as_ref().unwrap().next;
258+
slow = &mut slow.as_mut().unwrap().next;
259+
}
260+
slow.as_mut().unwrap().next = slow.as_mut().unwrap().next.as_mut().unwrap().next.take();
261+
dummy.unwrap().next
262+
}
263+
}
264+
```
265+
229266
### **...**
230267

231268
```

solution/0000-0099/0019.Remove Nth Node From End of List/README_EN.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,43 @@ def remove_nth_from_end(head, n)
214214
end
215215
```
216216

217+
### **Rust**
218+
219+
```rust
220+
// Definition for singly-linked list.
221+
// #[derive(PartialEq, Eq, Clone, Debug)]
222+
// pub struct ListNode {
223+
// pub val: i32,
224+
// pub next: Option<Box<ListNode>>
225+
// }
226+
//
227+
// impl ListNode {
228+
// #[inline]
229+
// fn new(val: i32) -> Self {
230+
// ListNode {
231+
// next: None,
232+
// val
233+
// }
234+
// }
235+
// }
236+
impl Solution {
237+
pub fn remove_nth_from_end(head: Option<Box<ListNode>>, n: i32) -> Option<Box<ListNode>> {
238+
let mut dummy = Some(Box::new(ListNode { val: 0, next: head }));
239+
let mut slow = &mut dummy;
240+
let mut fast = &slow.clone();
241+
for _ in 0..=n {
242+
fast = &fast.as_ref().unwrap().next;
243+
}
244+
while fast.is_some() {
245+
fast = &fast.as_ref().unwrap().next;
246+
slow = &mut slow.as_mut().unwrap().next;
247+
}
248+
slow.as_mut().unwrap().next = slow.as_mut().unwrap().next.as_mut().unwrap().next.take();
249+
dummy.unwrap().next
250+
}
251+
}
252+
```
253+
217254
### **...**
218255

219256
```
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Definition for singly-linked list.
2+
// #[derive(PartialEq, Eq, Clone, Debug)]
3+
// pub struct ListNode {
4+
// pub val: i32,
5+
// pub next: Option<Box<ListNode>>
6+
// }
7+
//
8+
// impl ListNode {
9+
// #[inline]
10+
// fn new(val: i32) -> Self {
11+
// ListNode {
12+
// next: None,
13+
// val
14+
// }
15+
// }
16+
// }
17+
impl Solution {
18+
pub fn remove_nth_from_end(head: Option<Box<ListNode>>, n: i32) -> Option<Box<ListNode>> {
19+
let mut dummy = Some(Box::new(ListNode { val: 0, next: head }));
20+
let mut slow = &mut dummy;
21+
let mut fast = &slow.clone();
22+
for _ in 0..=n {
23+
fast = &fast.as_ref().unwrap().next;
24+
}
25+
while fast.is_some() {
26+
fast = &fast.as_ref().unwrap().next;
27+
slow = &mut slow.as_mut().unwrap().next;
28+
}
29+
slow.as_mut().unwrap().next = slow.as_mut().unwrap().next.as_mut().unwrap().next.take();
30+
dummy.unwrap().next
31+
}
32+
}

0 commit comments

Comments
 (0)