diff --git a/solution/0000-0099/0092.Reverse Linked List II/README.md b/solution/0000-0099/0092.Reverse Linked List II/README.md index d82530416e6bd..9281a72ec2669 100644 --- a/solution/0000-0099/0092.Reverse Linked List II/README.md +++ b/solution/0000-0099/0092.Reverse Linked List II/README.md @@ -317,6 +317,48 @@ function reverseBetween(head: ListNode | null, left: number, right: number): Lis } ``` +### **Rust** + +```rust +// Definition for singly-linked list. +// #[derive(PartialEq, Eq, Clone, Debug)] +// pub struct ListNode { +// pub val: i32, +// pub next: Option> +// } +// +// impl ListNode { +// #[inline] +// fn new(val: i32) -> Self { +// ListNode { +// next: None, +// val +// } +// } +// } +impl Solution { + pub fn reverse_between(head: Option>, left: i32, right: i32) -> Option> { + let mut dummy = Some(Box::new(ListNode { val: 0, next: head })); + let mut pre = &mut dummy; + for _ in 1..left { + pre = &mut pre.as_mut().unwrap().next; + } + let mut cur = pre.as_mut().unwrap().next.take(); + for _ in 0..right - left + 1 { + let mut next = cur.as_mut().unwrap().next.take(); + cur.as_mut().unwrap().next = pre.as_mut().unwrap().next.take(); + pre.as_mut().unwrap().next = cur.take(); + cur = next; + } + for _ in 0..right - left + 1 { + pre = &mut pre.as_mut().unwrap().next; + } + pre.as_mut().unwrap().next = cur; + dummy.unwrap().next + } +} +``` + ### **...** ``` diff --git a/solution/0000-0099/0092.Reverse Linked List II/README_EN.md b/solution/0000-0099/0092.Reverse Linked List II/README_EN.md index c3150b5523449..684d3e908cd75 100644 --- a/solution/0000-0099/0092.Reverse Linked List II/README_EN.md +++ b/solution/0000-0099/0092.Reverse Linked List II/README_EN.md @@ -300,6 +300,48 @@ function reverseBetween(head: ListNode | null, left: number, right: number): Lis } ``` +### **Rust** + +```rust +// Definition for singly-linked list. +// #[derive(PartialEq, Eq, Clone, Debug)] +// pub struct ListNode { +// pub val: i32, +// pub next: Option> +// } +// +// impl ListNode { +// #[inline] +// fn new(val: i32) -> Self { +// ListNode { +// next: None, +// val +// } +// } +// } +impl Solution { + pub fn reverse_between(head: Option>, left: i32, right: i32) -> Option> { + let mut dummy = Some(Box::new(ListNode { val: 0, next: head })); + let mut pre = &mut dummy; + for _ in 1..left { + pre = &mut pre.as_mut().unwrap().next; + } + let mut cur = pre.as_mut().unwrap().next.take(); + for _ in 0..right - left + 1 { + let mut next = cur.as_mut().unwrap().next.take(); + cur.as_mut().unwrap().next = pre.as_mut().unwrap().next.take(); + pre.as_mut().unwrap().next = cur.take(); + cur = next; + } + for _ in 0..right - left + 1 { + pre = &mut pre.as_mut().unwrap().next; + } + pre.as_mut().unwrap().next = cur; + dummy.unwrap().next + } +} +``` + ### **...** ``` diff --git a/solution/0000-0099/0092.Reverse Linked List II/Solution.rs b/solution/0000-0099/0092.Reverse Linked List II/Solution.rs new file mode 100644 index 0000000000000..e7b023c4d60ca --- /dev/null +++ b/solution/0000-0099/0092.Reverse Linked List II/Solution.rs @@ -0,0 +1,37 @@ +// Definition for singly-linked list. +// #[derive(PartialEq, Eq, Clone, Debug)] +// pub struct ListNode { +// pub val: i32, +// pub next: Option> +// } +// +// impl ListNode { +// #[inline] +// fn new(val: i32) -> Self { +// ListNode { +// next: None, +// val +// } +// } +// } +impl Solution { + pub fn reverse_between(head: Option>, left: i32, right: i32) -> Option> { + let mut dummy = Some(Box::new(ListNode { val: 0, next: head })); + let mut pre = &mut dummy; + for _ in 1..left { + pre = &mut pre.as_mut().unwrap().next; + } + let mut cur = pre.as_mut().unwrap().next.take(); + for _ in 0..right - left + 1 { + let mut next = cur.as_mut().unwrap().next.take(); + cur.as_mut().unwrap().next = pre.as_mut().unwrap().next.take(); + pre.as_mut().unwrap().next = cur.take(); + cur = next; + } + for _ in 0..right - left + 1 { + pre = &mut pre.as_mut().unwrap().next; + } + pre.as_mut().unwrap().next = cur; + dummy.unwrap().next + } +}