-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 0725: Split Linked List in Parts
- Loading branch information
Showing
3 changed files
with
86 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
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; | ||
|
||
impl Solution { | ||
pub fn split_list_to_parts(head: Option<Box<ListNode>>, k: i32) -> Vec<Option<Box<ListNode>>> { | ||
let k = k as usize; | ||
let mut head = head; | ||
|
||
let mut result = Vec::with_capacity(k); | ||
let count = std::iter::successors(head.as_ref(), |x| x.next.as_ref()).count(); | ||
let size = count / k; | ||
|
||
for size in std::iter::repeat(size + 1) | ||
.take(count % k) | ||
.chain(std::iter::repeat(size).take(k - count % k)) | ||
{ | ||
if size == 0 { | ||
result.push(None); | ||
} else { | ||
let mut pointer = head.as_mut(); | ||
for _ in 0..size - 1 { | ||
if let Some(p) = pointer { | ||
pointer = p.next.as_mut(); | ||
} | ||
} | ||
let new_head = pointer.unwrap().next.take(); | ||
result.push(head); | ||
head = new_head; | ||
} | ||
} | ||
|
||
result | ||
} | ||
} | ||
|
||
impl super::Solution for Solution { | ||
fn split_list_to_parts(head: Option<Box<ListNode>>, k: i32) -> Vec<Option<Box<ListNode>>> { | ||
Self::split_list_to_parts(head, k) | ||
} | ||
} | ||
|
||
#[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,36 @@ | ||
pub mod iterative; | ||
|
||
use crate::data_structures::ListNode; | ||
|
||
pub trait Solution { | ||
fn split_list_to_parts(head: Option<Box<ListNode>>, k: i32) -> Vec<Option<Box<ListNode>>>; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
use crate::test_utilities; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
( | ||
(&[1, 2, 3] as &[_], 5), | ||
&[&[1] as &[_], &[2], &[3], &[], &[]] as &[&[_]], | ||
), | ||
( | ||
(&[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 3), | ||
&[&[1, 2, 3, 4], &[5, 6, 7], &[8, 9, 10]], | ||
), | ||
]; | ||
|
||
for ((head, k), expected) in test_cases { | ||
assert_eq!( | ||
S::split_list_to_parts(test_utilities::make_list(head.iter().copied()), k) | ||
.iter() | ||
.map(|list| test_utilities::iter_list(list).copied().collect::<Vec<_>>()) | ||
.collect::<Vec<_>>(), | ||
expected | ||
); | ||
} | ||
} | ||
} |