-
Notifications
You must be signed in to change notification settings - Fork 0
/
reverse_linked_list.rs
42 lines (36 loc) · 1.14 KB
/
reverse_linked_list.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
use crate::list_node::ListNode;
use crate::list_node_additions::ListNodeAdditions;
/// Given the `head` of a singly linked list, reverse the list, and return the reversed list.
struct Solution;
impl Solution {
pub fn reverse_list(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
head.reverse()
}
}
#[cfg(test)]
mod tests {
use super::Solution;
use crate::list_node::ListNode;
use crate::list_node_additions::ListNodeAdditions;
#[test]
fn example_1() {
let items = vec![1, 2, 3, 4, 5];
let nodes = ListNodeAdditions::from_vec(items);
let result = Solution::reverse_list(nodes);
assert_eq!(result.to_vec(), vec![5, 4, 3, 2, 1]);
}
#[test]
fn example_2() {
let items = vec![1, 2];
let nodes = ListNodeAdditions::from_vec(items);
let result = Solution::reverse_list(nodes);
assert_eq!(result.to_vec(), vec![2, 1]);
}
#[test]
fn example_3() {
let items = vec![];
let nodes = ListNodeAdditions::from_vec(items);
let result = Solution::reverse_list(nodes);
assert_eq!(result.to_vec(), vec![]);
}
}