- Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpalindrome-linked-list.rs
42 lines (34 loc) · 862 Bytes
/
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
#![allow(dead_code, unused, unused_variables)]
fnmain(){}
structSolution;
// Definition for singly-linked list.
#[derive(PartialEq,Eq,Clone,Debug)]
pubstructListNode{
pubval:i32,
pubnext:Option<Box<ListNode>>,
}
implListNode{
#[inline]
fnnew(val:i32) -> Self{
ListNode{next:None, val }
}
}
implSolution{
pubfnis_palindrome(head:Option<Box<ListNode>>) -> bool{
letmut v = vec![];
letmut head = head;
while head.is_some(){
v.push(head.as_ref().unwrap().val);
head = head.as_mut().unwrap().next.take();
}
let(mut star,mut end) = (0, v.len() - 1);
while star < end {
if v[star] != v[end]{
returnfalse;
}
star += 1;
end -= 1;
}
true
}
}