-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
69 lines (59 loc) · 1.48 KB
/
mod.rs
File metadata and controls
69 lines (59 loc) · 1.48 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
/// https://leetcode.com/problems/reverse-linked-list/
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct ListNode {
pub val: i32,
pub next: Option<Box<ListNode>>,
}
impl ListNode {
#[inline]
#[allow(dead_code)]
fn new(val: i32) -> Self {
ListNode { next: None, val }
}
#[allow(dead_code)]
fn new_node(val: i32, next: ListNode) -> Self {
ListNode {
next: Some(Box::new(next)),
val,
}
}
#[allow(dead_code)]
fn to_vec(self) -> Vec<i32> {
let mut result = Vec::new();
let mut current = Some(Box::new(self));
while current.is_some() {
if let Some(n) = current {
result.push(n.val);
current = n.next;
}
}
result
}
}
#[allow(dead_code)]
pub fn reverse_list(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
let mut it = head;
let mut prev = None;
while let Some(mut v) = it {
let mut next = v.next.take();
v.next = prev.take();
prev = Some(v);
it = next.take();
}
prev
}
#[cfg(test)]
mod tests {
use super::ListNode;
#[test]
fn reverse_list_test() {
let lst = ListNode::new_node(
1,
ListNode::new_node(
2,
ListNode::new_node(3, ListNode::new_node(4, ListNode::new(5))),
),
);
assert_eq!(vec![1, 2, 3, 4, 5], Some(Box::new(lst)).unwrap().to_vec());
}
}