From 5abbaeb0ce3317957098ab3d72ff3efbf9d31544 Mon Sep 17 00:00:00 2001 From: hthuz Date: Fri, 15 Nov 2024 15:05:55 +0800 Subject: [PATCH] add: rust reverse list --- rust/json/.gitignore | 1 + rust/list/.gitignore | 1 + rust/list/Cargo.lock | 7 +++ rust/list/Cargo.toml | 6 +++ rust/list/src/main.rs | 103 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 118 insertions(+) create mode 100644 rust/json/.gitignore create mode 100644 rust/list/.gitignore create mode 100644 rust/list/Cargo.lock create mode 100644 rust/list/Cargo.toml create mode 100644 rust/list/src/main.rs diff --git a/rust/json/.gitignore b/rust/json/.gitignore new file mode 100644 index 0000000..eb5a316 --- /dev/null +++ b/rust/json/.gitignore @@ -0,0 +1 @@ +target diff --git a/rust/list/.gitignore b/rust/list/.gitignore new file mode 100644 index 0000000..eb5a316 --- /dev/null +++ b/rust/list/.gitignore @@ -0,0 +1 @@ +target diff --git a/rust/list/Cargo.lock b/rust/list/Cargo.lock new file mode 100644 index 0000000..acdbfec --- /dev/null +++ b/rust/list/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "list" +version = "0.1.0" diff --git a/rust/list/Cargo.toml b/rust/list/Cargo.toml new file mode 100644 index 0000000..2e92fb9 --- /dev/null +++ b/rust/list/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "list" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/rust/list/src/main.rs b/rust/list/src/main.rs new file mode 100644 index 0000000..e80ae76 --- /dev/null +++ b/rust/list/src/main.rs @@ -0,0 +1,103 @@ + + +// Definition for singly-linked list. +#[derive(Clone, Debug)] +pub struct ListNode { + pub val: i32, + pub next: Option> +} +// +impl ListNode { + #[inline] + fn new(val: i32) -> Self { + ListNode { + next: None, + val + } + } +} + +struct Solution(); +impl Solution { + pub fn swap_pairs(head: Option>) -> Option> { + let mut cur = head?; + if let Some(mut next) = cur.next { + cur.next = Solution::swap_pairs(next.next); + next.next = Some(cur); + return Some(next) + } + Some(cur) + } + + + pub fn reverse_between(head: Option>, left: i32, right: i32) -> Option> { + + let mut head = head; + let mut left_prev_node = &mut head; + for _ in 0..left-2{ + left_prev_node = &mut left_prev_node.as_mut().unwrap().next; + } + let mut left_prev_node_ptr = left_prev_node.as_mut().unwrap(); + // if let Some(left_node_ptr) = left_node { + left_prev_node_ptr.next = Solution::reverse(left_prev_node_ptr.next.take()); + // } + + head + + } + + + + pub fn _reverse(prev: Option>, cur: Option>) -> Option> { + let mut cur_node = cur?; + let next = cur_node.next; + cur_node.next = prev; + if next.is_none() { + return Some(cur_node); + } + Solution::_reverse(Some(cur_node), next) + } + + pub fn reverse(head: Option>) -> Option> { + Solution::_reverse(None, head) + } + + + pub fn print_list(head: &Option>) { + let mut cur = head; + while let Some(cur_ptr) = cur { + print!("{} ",cur_ptr.val); + cur = &cur_ptr.next; + } + println!() + } +} + +fn main() { + // let list = None; + let list = Some(Box::new(ListNode{ + val: 1, + // next: None, + next: Some(Box::new(ListNode{ + val:2, + next: Some(Box::new(ListNode{ + val:3, + // next: None, + next:Some(Box::new(ListNode{ + val:4, + next: Some(Box::new(ListNode{ + val: 5, + next: None + })) + })) + })) + })) + })); + + Solution::print_list(&list); + let new_list = Solution::reverse_between(list, 2,4); + Solution::print_list(&new_list); + + + +}