-
Notifications
You must be signed in to change notification settings - Fork 1
/
234.palindrome-linked-list.rs
75 lines (62 loc) · 1.67 KB
/
234.palindrome-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
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
/*
* @lc app=leetcode id=234 lang=rust
*
* [234] Palindrome Linked List
*/
// @lc code=start
// 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
// }
// }
// }
impl Solution {
pub fn is_palindrome(mut head: Option<Box<ListNode>>) -> bool {
if head.is_none() { return true; }
let mut N = 0;
let mut href = head.as_ref();
while let Some(node) = href {
href = node.next.as_ref();
N += 1;
}
let mut hmut = head.as_mut();
let mut mid = N / 2 - 1;
while mid > 0 {
hmut = hmut.unwrap().next.as_mut();
mid -= 1;
}
let mut prev = None;
let mut next = hmut.unwrap().next.take();
while let Some(mut next_inner) = next {
next = next_inner.next.take();
next_inner.next = prev.take();
prev = Some(next_inner);
}
let mut h1 = &head;
let mut h2 = &prev;
loop {
let (not_equal, should_break) = match (h1.as_ref(), h2.as_ref()) {
(Some(h1_inner), Some(h2_inner)) => {
h1 = &h1_inner.next;
h2 = &h2_inner.next;
(h1_inner.val != h2_inner.val, false)
},
_ => (false, true)
};
if should_break { break; }
if not_equal { return false; }
}
return true;
}
}
// @lc code=end