-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.rs
More file actions
180 lines (170 loc) · 6 KB
/
main.rs
File metadata and controls
180 lines (170 loc) · 6 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
fn main() {
let n5 = ListNode::new(5);
let n4 = ListNode { val: 4, next: Some(Box::new(n5)) };
let n3 = ListNode { val: 3, next: Some(Box::new(n4)) };
let n2 = ListNode { val: 2, next: Some(Box::new(n3)) };
let n1 = ListNode { val: 1, next: Some(Box::new(n2)) };
let head1 = Some(Box::new(n1));
let n5 = ListNode::new(5);
let n4 = ListNode { val: 3, next: Some(Box::new(n5)) };
let n3 = ListNode { val: 4, next: Some(Box::new(n4)) };
let n2 = ListNode { val: 1, next: Some(Box::new(n3)) };
let n1 = ListNode { val: 2, next: Some(Box::new(n2)) };
let head2 = Some(Box::new(n1));
Solution::reverse_k_group(head1, 2);
}
// Definition for singly-linked list.
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct ListNode {
pub val: i32,
pub next: Option<Box<ListNode>>
}
impl ListNode {
#[inline]
fn new(val: i32) -> Self {
ListNode {
next: None,
val
}
}
}
struct Solution {}
impl Solution {
/// 0 < k <= len, list is not empty
pub fn reverse_k_group(head: Option<Box<ListNode>>, k: i32) -> Option<Box<ListNode>> {
if head.is_none() { return head }
let mut buf: Vec<Box<ListNode>> = Vec::with_capacity(k as usize);
let mut nodeptrd = head;
for _ in 0..k {
if let Some(mut node) = nodeptrd {
nodeptrd = node.next.take();
buf.push(node);
} else {
break
}
}
let end = if buf.len() < k as usize { true } else { false };
if end {
// in the origin order
buf = buf.into_iter().rev().collect();
}
let mut head = Some(buf.pop().unwrap());
let mut nodeptr = head.as_mut().unwrap();
// construct the list from 1 to n
while let Some(node) = buf.pop() {
nodeptr.next = Some(node);
nodeptr = nodeptr.next.as_mut().unwrap();
}
if nodeptrd.is_none() { return head }
loop {
for _ in 0..k {
if let Some(mut node) = nodeptrd {
nodeptrd = node.next.take();
buf.push(node);
} else {
break
}
// println!("buf = {:#?}", buf);
}
let end = if buf.len() < k as usize { true } else { false };
if end {
// in the origin order
buf = buf.into_iter().rev().collect();
}
while let Some(node) = buf.pop() {
nodeptr.next = Some(node);
nodeptr = nodeptr.next.as_mut().unwrap();
}
if end { break }
}
// println!("head = {:#?}", head);
head
}
}
#[cfg(test)]
mod test {
use crate::*;
#[test]
fn basic1() {
let n5 = ListNode::new(5);
let n4 = ListNode { val: 4, next: Some(Box::new(n5)) };
let n3 = ListNode { val: 3, next: Some(Box::new(n4)) };
let n2 = ListNode { val: 2, next: Some(Box::new(n3)) };
let n1 = ListNode { val: 1, next: Some(Box::new(n2)) };
let head1 = Some(Box::new(n1));
let n5 = ListNode::new(1);
let n4 = ListNode { val: 2, next: Some(Box::new(n5)) };
let n3 = ListNode { val: 3, next: Some(Box::new(n4)) };
let n2 = ListNode { val: 4, next: Some(Box::new(n3)) };
let n1 = ListNode { val: 5, next: Some(Box::new(n2)) };
let head2 = Some(Box::new(n1));
assert_eq!(
Solution::reverse_k_group(head1, 5),
head2
);
}
#[test]
fn basic2() {
let n5 = ListNode::new(5);
let n4 = ListNode { val: 4, next: Some(Box::new(n5)) };
let n3 = ListNode { val: 3, next: Some(Box::new(n4)) };
let n2 = ListNode { val: 2, next: Some(Box::new(n3)) };
let n1 = ListNode { val: 1, next: Some(Box::new(n2)) };
let head1 = Some(Box::new(n1));
let n5 = ListNode::new(5);
let n4 = ListNode { val: 3, next: Some(Box::new(n5)) };
let n3 = ListNode { val: 4, next: Some(Box::new(n4)) };
let n2 = ListNode { val: 1, next: Some(Box::new(n3)) };
let n1 = ListNode { val: 2, next: Some(Box::new(n2)) };
let head2 = Some(Box::new(n1));
assert_eq!(
Solution::reverse_k_group(head1, 2),
head2
);
}
#[test]
fn basic3() {
let n5 = ListNode::new(5);
let n4 = ListNode { val: 4, next: Some(Box::new(n5)) };
let n3 = ListNode { val: 3, next: Some(Box::new(n4)) };
let n2 = ListNode { val: 2, next: Some(Box::new(n3)) };
let n1 = ListNode { val: 1, next: Some(Box::new(n2)) };
let head1 = Some(Box::new(n1));
let n5 = ListNode::new(5);
let n4 = ListNode { val: 4, next: Some(Box::new(n5)) };
let n3 = ListNode { val: 1, next: Some(Box::new(n4)) };
let n2 = ListNode { val: 2, next: Some(Box::new(n3)) };
let n1 = ListNode { val: 3, next: Some(Box::new(n2)) };
let head2 = Some(Box::new(n1));
assert_eq!(
Solution::reverse_k_group(head1, 3),
head2
);
}
#[test]
fn none() {
assert_eq!(
Solution::reverse_k_group(None, 1),
None
);
}
#[test]
fn name() {
let n5 = ListNode::new(5);
let n4 = ListNode { val: 4, next: Some(Box::new(n5)) };
let n3 = ListNode { val: 3, next: Some(Box::new(n4)) };
let n2 = ListNode { val: 2, next: Some(Box::new(n3)) };
let n1 = ListNode { val: 1, next: Some(Box::new(n2)) };
let head1 = Some(Box::new(n1));
let n5 = ListNode::new(5);
let n4 = ListNode { val: 4, next: Some(Box::new(n5)) };
let n3 = ListNode { val: 3, next: Some(Box::new(n4)) };
let n2 = ListNode { val: 2, next: Some(Box::new(n3)) };
let n1 = ListNode { val: 1, next: Some(Box::new(n2)) };
let head2 = Some(Box::new(n1));
assert_eq!(
Solution::reverse_k_group(head1, 123),
head2
);
}
}