diff --git a/src/list.rs b/src/list.rs index 26ce729..77e55bb 100644 --- a/src/list.rs +++ b/src/list.rs @@ -7,7 +7,7 @@ pub struct ListNode { impl ListNode { #[inline] - fn new(val: i32) -> Self { + pub fn new(val: i32) -> Self { ListNode { next: None, val diff --git a/src/main.rs b/src/main.rs index 66c5b2a..f1b8450 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,28 @@ +use crate::list::{ListNode, make_list}; + mod arr; +mod list; mod tree; struct Solution {} impl Solution { - pub fn reverse_words(s: String) -> String { - s.split(' ').filter(|s| !s.is_empty()).rev().collect::>().join(" ") + pub fn training_plan(head: Option>, cnt: i32) -> Option> { + 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() { - println!("{:?}", Solution::reverse_words("a good example".to_string())); + println!( + "{:?}", + Solution::training_plan(make_list(vec![2, 4, 7, 8]), 2) + ); }