-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathreverse_k_group.rs
160 lines (142 loc) · 4.76 KB
/
reverse_k_group.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
use std::{cell::RefCell, rc::Rc};
use super::{ListNode, ListNode2};
pub fn reverse_k_group(head: Option<Box<ListNode>>, k: i32) -> Option<Box<ListNode>> {
if k <= 1 {
return head;
}
// 1. 从head开始,确认其后至少包含k个节点,否则直接返回head
let mut head = head;
let mut curr = &mut head;
for _ in 0..k - 1 {
if curr.is_none() || curr.as_ref().unwrap().next.is_none() {
return head;
}
curr = &mut curr.as_mut().unwrap().next;
}
// 2. 反转前k个节点
let mut prev: Option<Box<ListNode>> = None;
let mut curr = head;
for _ in 0..k {
let next = curr.as_mut().unwrap().next.take();
curr.as_mut().unwrap().next = prev.take();
prev = curr;
curr = next;
}
// 3. 找到新的末尾节点,并设置其next为后续新递归结果的头节点
// 注意:这种算法明显不是最优的,因为每次都需要在这里循环找到尾节点,更好的方式可能是使用ListNode2这样的结构
let mut tail = &mut prev;
for i in 0..k - 1 {
tail = &mut tail.as_mut().unwrap().next;
if i == k - 2 {
break;
}
}
tail.as_mut().unwrap().next = reverse_k_group(curr, k);
prev
}
/// 另一种通过`Rc`, `RefCell`实现的版本
pub fn reverse_k_group2(
head: Option<Rc<RefCell<ListNode2>>>,
k: i32,
) -> Option<Rc<RefCell<ListNode2>>> {
if k <= 1 {
return head;
}
// 1. 从head开始,确认其后至少包含k个节点,否则直接返回head
let mut curr = head.clone();
for _ in 0..k - 1 {
if curr.is_none() || curr.as_ref().unwrap().borrow().next.is_none() {
return head.clone();
}
let next = curr.as_deref().unwrap().borrow().next.clone();
curr = next;
}
// 2. 反转前k个节点
let mut curr = head.clone();
let mut prev = None;
for _ in 0..k {
let next = curr.as_ref().unwrap().borrow_mut().next.take();
curr.as_ref().unwrap().borrow_mut().next = prev.take();
prev = curr;
curr = next;
}
// 3. 将翻转后的尾节点的next指向后续递归翻转后的头节点
let new_tail = head.clone();
new_tail.as_ref().unwrap().borrow_mut().next = reverse_k_group2(curr, k);
// 返回翻转后的头节点
prev
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_reverse_k_group() {
let ret = reverse_k_group(None, 2);
assert!(ret.is_none());
let head = Some(Box::new(ListNode {
val: 1,
next: Some(Box::new(ListNode {
val: 2,
next: Some(Box::new(ListNode {
val: 3,
next: Some(Box::new(ListNode {
val: 4,
next: Some(Box::new(ListNode { val: 5, next: None })),
})),
})),
})),
}));
let ret = reverse_k_group(head, 2);
assert_eq!(
ret,
Some(Box::new(ListNode {
val: 2,
next: Some(Box::new(ListNode {
val: 1,
next: Some(Box::new(ListNode {
val: 4,
next: Some(Box::new(ListNode {
val: 3,
next: Some(Box::new(ListNode { val: 5, next: None })),
})),
})),
})),
}))
);
}
#[test]
fn test_reverse_k_group2() {
let ret = reverse_k_group2(None, 2);
assert!(ret.is_none());
let head = Some(Rc::new(RefCell::new(ListNode2 {
val: 1,
next: Some(Rc::new(RefCell::new(ListNode2 {
val: 2,
next: Some(Rc::new(RefCell::new(ListNode2 {
val: 3,
next: Some(Rc::new(RefCell::new(ListNode2 {
val: 4,
next: Some(Rc::new(RefCell::new(ListNode2 { val: 5, next: None }))),
}))),
}))),
}))),
})));
let ret = reverse_k_group2(head, 2);
assert_eq!(
ret,
Some(Rc::new(RefCell::new(ListNode2 {
val: 2,
next: Some(Rc::new(RefCell::new(ListNode2 {
val: 1,
next: Some(Rc::new(RefCell::new(ListNode2 {
val: 4,
next: Some(Rc::new(RefCell::new(ListNode2 {
val: 3,
next: Some(Rc::new(RefCell::new(ListNode2 { val: 5, next: None }))),
}))),
}))),
}))),
})))
);
}
}