-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 2074: Reverse Nodes in Even Length Groups
- Loading branch information
Showing
3 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
src/problem_2074_reverse_nodes_in_even_length_groups/iterative.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
use crate::data_structures::ListNode; | ||
|
||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
use std::mem; | ||
|
||
impl Solution { | ||
fn reverse(mut head: Option<Box<ListNode>>) -> Option<Box<ListNode>> { | ||
let mut result = None; | ||
|
||
while let Some(mut node) = head { | ||
head = mem::replace(&mut node.next, result); | ||
result = Some(node); | ||
} | ||
|
||
result | ||
} | ||
|
||
fn append(mut tail: &mut Option<Box<ListNode>>, mut head: Option<Box<ListNode>>) -> &mut Option<Box<ListNode>> { | ||
while let Some(mut node) = head { | ||
head = node.next.take(); | ||
tail = &mut tail.insert(node).next; | ||
} | ||
|
||
tail | ||
} | ||
|
||
pub fn reverse_even_length_groups(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> { | ||
let mut head = head; | ||
let mut result = None; | ||
let mut tail = &mut result; | ||
let mut group_length = 0_u32; | ||
let mut group_head; | ||
let mut group_tail; | ||
let mut required; | ||
|
||
'outer: loop { | ||
// Odd group. | ||
|
||
group_length += 1; | ||
group_head = None; | ||
group_tail = &mut group_head; | ||
required = group_length; | ||
|
||
loop { | ||
if let Some(mut node) = head { | ||
head = node.next.take(); | ||
group_tail = &mut group_tail.insert(node).next; | ||
required -= 1; | ||
|
||
if required == 0 { | ||
tail = Self::append(tail, group_head); | ||
|
||
break; | ||
} | ||
} else { | ||
break 'outer; | ||
} | ||
} | ||
|
||
// Even group. | ||
|
||
group_length += 1; | ||
group_head = None; | ||
required = group_length; | ||
|
||
loop { | ||
if let Some(mut node) = head { | ||
head = mem::replace(&mut node.next, group_head); | ||
group_head = Some(node); | ||
required -= 1; | ||
|
||
if required == 0 { | ||
tail = Self::append(tail, group_head); | ||
|
||
break; | ||
} | ||
} else { | ||
break 'outer; | ||
} | ||
} | ||
} | ||
|
||
// Last group. | ||
|
||
if required % 2 != 0 { | ||
group_head = Self::reverse(group_head); | ||
} | ||
|
||
Self::append(tail, group_head); | ||
|
||
result | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn reverse_even_length_groups(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> { | ||
Self::reverse_even_length_groups(head) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/problem_2074_reverse_nodes_in_even_length_groups/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use crate::data_structures::ListNode; | ||
|
||
pub mod iterative; | ||
|
||
pub trait Solution { | ||
fn reverse_even_length_groups(head: Option<Box<ListNode>>) -> Option<Box<ListNode>>; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
use crate::test_utilities; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
( | ||
&[5, 2, 6, 3, 9, 1, 7, 3, 8, 4] as &[_], | ||
&[5, 6, 2, 3, 9, 1, 4, 8, 3, 7] as &[_], | ||
), | ||
(&[1, 1, 0, 6], &[1, 0, 1, 6]), | ||
(&[1, 1, 0, 6, 5], &[1, 0, 1, 5, 6]), | ||
(&[2, 1], &[2, 1]), | ||
]; | ||
|
||
for (head, expected) in test_cases { | ||
assert_eq!( | ||
&test_utilities::iter_list(&S::reverse_even_length_groups(test_utilities::make_list( | ||
head.iter().copied() | ||
))) | ||
.copied() | ||
.collect::<Vec<_>>(), | ||
expected, | ||
); | ||
} | ||
} | ||
} |