LCR 140. 训练计划 II

This commit is contained in:
2026-08-14 20:00:49 +08:00
parent a813a0b1d6
commit d716101d77
2 changed files with 20 additions and 4 deletions
+1 -1
View File
@@ -7,7 +7,7 @@ pub struct ListNode {
impl ListNode { impl ListNode {
#[inline] #[inline]
fn new(val: i32) -> Self { pub fn new(val: i32) -> Self {
ListNode { ListNode {
next: None, next: None,
val val
+19 -3
View File
@@ -1,12 +1,28 @@
use crate::list::{ListNode, make_list};
mod arr; mod arr;
mod list;
mod tree; mod tree;
struct Solution {} struct Solution {}
impl Solution { impl Solution {
pub fn reverse_words(s: String) -> String { pub fn training_plan(head: Option<Box<ListNode>>, cnt: i32) -> Option<Box<ListNode>> {
s.split(' ').filter(|s| !s.is_empty()).rev().collect::<Vec<_>>().join(" ") let mut fast = &head;
for i in 0..cnt {
fast = &fast.as_ref()?.next;
}
let mut slow = &head;
while fast.is_some() {
slow = &slow.as_ref()?.next;
fast = &fast.as_ref()?.next;
}
slow.clone()
} }
} }
fn main() { fn main() {
println!("{:?}", Solution::reverse_words("a good example".to_string())); println!(
"{:?}",
Solution::training_plan(make_list(vec![2, 4, 7, 8]), 2)
);
} }