Skip to content

Commit

Permalink
Add problem 2074: Reverse Nodes in Even Length Groups
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Jun 16, 2024
1 parent dd9b7f2 commit a8f2e61
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1507,6 +1507,7 @@ pub mod problem_2068_check_whether_two_strings_are_almost_equivalent;
pub mod problem_2069_walking_robot_simulation_ii;
pub mod problem_2070_most_beautiful_item_for_each_query;
pub mod problem_2073_time_needed_to_buy_tickets;
pub mod problem_2074_reverse_nodes_in_even_length_groups;
pub mod problem_2078_two_furthest_houses_with_different_colors;
pub mod problem_2085_count_common_words_with_one_occurrence;
pub mod problem_2089_find_target_indices_after_sorting_array;
Expand Down
112 changes: 112 additions & 0 deletions src/problem_2074_reverse_nodes_in_even_length_groups/iterative.rs
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 src/problem_2074_reverse_nodes_in_even_length_groups/mod.rs
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,
);
}
}
}

0 comments on commit a8f2e61

Please sign in to comment.