-
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 2181: Merge Nodes in Between Zeros
- Loading branch information
Showing
3 changed files
with
79 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
49 changes: 49 additions & 0 deletions
49
src/problem_2181_merge_nodes_in_between_zeros/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,49 @@ | ||
use crate::data_structures::ListNode; | ||
|
||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl Solution { | ||
pub fn merge_nodes(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> { | ||
let mut result = None; | ||
let mut tail = &mut result; | ||
let mut iter = head; | ||
|
||
while let Some(mut node) = iter { | ||
let mut sum = node.val; | ||
|
||
loop { | ||
node = node.next.unwrap(); | ||
|
||
if node.val == 0 { | ||
iter = node.next.take(); | ||
node.val = sum; | ||
tail = &mut tail.insert(node).next; | ||
|
||
break; | ||
} | ||
|
||
sum += node.val; | ||
} | ||
} | ||
|
||
result | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn merge_nodes(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> { | ||
Self::merge_nodes(head) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
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,29 @@ | ||
use crate::data_structures::ListNode; | ||
|
||
pub mod iterative; | ||
|
||
pub trait Solution { | ||
fn merge_nodes(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 = [ | ||
(&[0, 3, 1, 0, 4, 5, 2, 0], &[4, 11] as &[_]), | ||
(&[0, 1, 0, 3, 0, 2, 2, 0], &[1, 3, 4]), | ||
]; | ||
|
||
for (head, expected) in test_cases { | ||
assert_eq!( | ||
&test_utilities::iter_list(&S::merge_nodes(test_utilities::make_list(head.iter().copied()))) | ||
.copied() | ||
.collect::<Vec<_>>(), | ||
expected, | ||
); | ||
} | ||
} | ||
} |