Skip to content

Commit 671aaff

Browse files
author
Zhang Xiaodong
committed
solve 25
1 parent 1793d32 commit 671aaff

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed

src/solution/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ mod s0021_merge_two_sorted_lists;
2020
mod s0022_generate_parentheses;
2121
mod s0023_merge_k_sorted_lists;
2222
mod s0024_swap_nodes_in_pairs;
23+
mod s0025_reverse_nodes_in_k_group;
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/**
2+
* [25] K 个一组翻转链表
3+
*
4+
* 给你链表的头节点 head ,每 k 个节点一组进行翻转,请你返回修改后的链表。
5+
* k 是一个正整数,它的值小于或等于链表的长度。如果节点总数不是 k 的整数倍,那么请将最后剩余的节点保持原有顺序。
6+
* 你不能只是单纯的改变节点内部的值,而是需要实际进行节点交换。
7+
*
8+
* 示例 1:
9+
* <img alt="" src="https://assets.leetcode.com/uploads/2020/10/03/reverse_ex1.jpg" style="width: 542px; height: 222px;" />
10+
* 输入:head = [1,2,3,4,5], k = 2
11+
* 输出:[2,1,4,3,5]
12+
*
13+
* 示例 2:
14+
* <img alt="" src="https://assets.leetcode.com/uploads/2020/10/03/reverse_ex2.jpg" style="width: 542px; height: 222px;" />
15+
*
16+
* 输入:head = [1,2,3,4,5], k = 3
17+
* 输出:[3,2,1,4,5]
18+
*
19+
*
20+
* 提示:
21+
*
22+
* 链表中的节点数目为 n
23+
* 1 <= k <= n <= 5000
24+
* 0 <= Node.val <= 1000
25+
*
26+
*
27+
* 进阶:你可以设计一个只用 O(1) 额外内存空间的算法解决此问题吗?
28+
*
29+
*
30+
*/
31+
pub struct Solution {}
32+
use crate::util::linked_list::{to_list, ListNode};
33+
34+
// problem: https://leetcode.cn/problems/reverse-nodes-in-k-group/
35+
// discuss: https://leetcode.cn/problems/reverse-nodes-in-k-group/discuss/?currentPage=1&orderBy=most_votes&query=
36+
37+
// submission codes start here
38+
39+
// Definition for singly-linked list.
40+
// #[derive(PartialEq, Eq, Clone, Debug)]
41+
// pub struct ListNode {
42+
// pub val: i32,
43+
// pub next: Option<Box<ListNode>>
44+
// }
45+
//
46+
// impl ListNode {
47+
// #[inline]
48+
// fn new(val: i32) -> Self {
49+
// ListNode {
50+
// next: None,
51+
// val
52+
// }
53+
// }
54+
// }
55+
impl Solution {
56+
pub fn reverse_k_group(head: Option<Box<ListNode>>, k: i32) -> Option<Box<ListNode>> {
57+
if k < 2 {
58+
return head;
59+
}
60+
let mut dummy_head = Box::new(ListNode { val: 0, next: head });
61+
unsafe {
62+
let mut fast = &mut dummy_head as *mut Box<ListNode>;
63+
let mut head = &mut dummy_head as *mut Box<ListNode>;
64+
let mut m: Option<Box<ListNode>> = None;
65+
'outer: loop {
66+
for _ in (0..k) {
67+
if (*fast).next.as_ref().is_none() {
68+
break 'outer;
69+
}
70+
fast = (*fast).as_mut().next.as_mut().unwrap();
71+
}
72+
m = (*head).as_mut().next.take();
73+
for _ in (0..k) {
74+
let n = m.as_mut().unwrap().next.take();
75+
m.as_mut().unwrap().next = (*head).as_mut().next.take();
76+
(*head).as_mut().next = m;
77+
m = n;
78+
}
79+
for _ in 0..k {
80+
head = (*head).as_mut().next.as_mut().unwrap();
81+
}
82+
head.as_mut().unwrap().next = m;
83+
fast = head;
84+
}
85+
}
86+
dummy_head.next
87+
}
88+
}
89+
90+
// submission codes end
91+
92+
#[cfg(test)]
93+
mod tests {
94+
use super::*;
95+
96+
#[test]
97+
fn test_25() {
98+
assert_eq!(
99+
Solution::reverse_k_group(to_list(vec![1]), 1),
100+
to_list(vec![1])
101+
);
102+
assert_eq!(
103+
Solution::reverse_k_group(to_list(vec![1, 2, 3, 4, 5, 6]), 3),
104+
to_list(vec![3, 2, 1, 6, 5, 4])
105+
);
106+
assert_eq!(
107+
Solution::reverse_k_group(to_list(vec![1, 2, 3, 4, 5]), 3),
108+
to_list(vec![3, 2, 1, 4, 5])
109+
);
110+
assert_eq!(
111+
Solution::reverse_k_group(to_list(vec![1, 2, 3, 4, 5]), 2),
112+
to_list(vec![2, 1, 4, 3, 5])
113+
);
114+
assert_eq!(
115+
Solution::reverse_k_group(to_list(vec![1, 2]), 2),
116+
to_list(vec![2, 1])
117+
);
118+
}
119+
}

0 commit comments

Comments
 (0)