题解 | #判断一个链表是否为回文结构# | Rust
判断一个链表是否为回文结构
https://www.nowcoder.com/practice/3fed228444e740c8be66232ce8b87c2f
/** * #[derive(PartialEq, Eq, Debug, Clone)] * pub struct ListNode { * pub val: i32, * pub next: Option<Box<ListNode>> * } * * impl ListNode { * #[inline] * fn new(val: i32) -> Self { * ListNode { * val: val, * next: None, * } * } * } */ struct Solution{ } impl Solution { fn new() -> Self { Solution{} } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 the head * @return bool布尔型 */ pub fn isPail(&self, head: Option<Box<ListNode>>) -> bool { let mut nodes : Vec<i32> = Vec::new(); let mut head = head; while head.is_none() == false { nodes.push(head.as_ref().unwrap().val); head = head.as_mut().unwrap().next.take(); } let (mut l, mut r) = (0, nodes.len()-1); while l < r { if nodes[l] != nodes[r] { return false; } l+=1; r-=1; } return true; } }