forked from neetcode-gh/leetcode
-
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.
- Loading branch information
1 parent
36d75f8
commit b4f85b9
Showing
1 changed file
with
48 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
impl Solution { | ||
pub fn reverse_k_group(head: Option<Box<ListNode>>, k: i32) -> Option<Box<ListNode>> { | ||
let mut dummy = Some(Box::new(ListNode {next: head, val: 0 })); | ||
let mut cur = dummy.as_mut(); | ||
|
||
'outer: loop { | ||
let mut start = cur.as_mut().unwrap().next.take(); | ||
if start.is_none() { | ||
break 'outer; | ||
} | ||
|
||
let mut end = start.as_mut(); | ||
for _ in 0..(k - 1) { | ||
end = end.unwrap().next.as_mut(); | ||
|
||
if end.is_none() { | ||
cur.as_mut().unwrap().next = start; | ||
break 'outer; | ||
} | ||
} | ||
|
||
let mut tail = end.as_mut().unwrap().next.take(); | ||
let end = Solution::reverse(start, tail); | ||
cur.as_mut().unwrap().next = end; | ||
|
||
for _ in 0..k { | ||
cur = cur.unwrap().next.as_mut() | ||
} | ||
} | ||
dummy.unwrap().next | ||
} | ||
|
||
fn reverse( | ||
mut head: Option<Box<ListNode>>, | ||
tail: Option<Box<ListNode>>, | ||
) -> Option<Box<ListNode>> { | ||
let mut prev = tail; | ||
let mut cur = head; | ||
|
||
while let Some(mut cur_node) = cur { | ||
let mut next = cur_node.next.take(); | ||
cur_node.next = prev.take(); | ||
prev = Some(cur_node); | ||
cur = next | ||
} | ||
prev | ||
} | ||
} |