-
Notifications
You must be signed in to change notification settings - Fork 9
/
linked_list_is_palindrome.rs
41 lines (36 loc) · 1.03 KB
/
linked_list_is_palindrome.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
use super::ListNode;
fn is_palindrome(head: Option<Box<ListNode>>) -> bool {
let mut nums = Vec::new();
let mut curr = &head;
while let Some(curr_inner) = curr {
nums.push(curr_inner.val);
curr = &curr_inner.next;
}
let nums_before_reverse = nums.clone();
nums.reverse();
nums_before_reverse == nums
}
#[cfg(test)]
const TEST_CASES: [(&[i32], bool); 1] = [(&[0, 0], true)];
#[test]
fn test_linked_list_is_palindrome() {
for (ln, expected) in TEST_CASES {
let head = super::arr_to_linked_list(ln);
assert_eq!(is_palindrome(head), expected);
}
}
// 仅仅适用于node.val在0~9之间
#[cfg(FALSE)]
fn is_palindrome_only_one_digit(head: Option<Box<ListNode>>) -> bool {
let mut asc = 0;
let mut desc = 0;
let mut desc_base = 1;
let mut curr = head.as_ref();
while let Some(curr_node) = curr {
asc = 10 * asc + curr_node.val;
desc += curr_node.val * desc_base;
desc_base *= 10;
curr = curr_node.next.as_ref();
}
asc == desc
}