Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 0cfc6f9

Browse files
committed
src/bin/palindrome-linked-list.rs
1 parent 9f4165f commit 0cfc6f9

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

src/bin/palindrome-linked-list.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
fn main() {}
2+
3+
struct Solution;
4+
5+
// Definition for singly-linked list.
6+
#[derive(PartialEq, Eq, Clone, Debug)]
7+
pub struct ListNode {
8+
pub val: i32,
9+
pub next: Option<Box<ListNode>>,
10+
}
11+
12+
impl ListNode {
13+
#[inline]
14+
fn new(val: i32) -> Self {
15+
ListNode {
16+
next: None,
17+
val,
18+
}
19+
}
20+
}
21+
22+
impl Solution {
23+
pub fn is_palindrome(head: Option<Box<ListNode>>) -> bool {
24+
let mut v = vec![];
25+
let mut head = head;
26+
27+
while head.is_some() {
28+
v.push(head.as_ref().unwrap().val);
29+
head = head.as_mut().unwrap().next.take();
30+
}
31+
32+
let (mut star, mut end) = (0, v.len() - 1);
33+
while star < end {
34+
if v[star] != v[end] {
35+
return false;
36+
}
37+
star += 1;
38+
end -= 1;
39+
}
40+
41+
true
42+
}
43+
}

0 commit comments

Comments
 (0)