题解 | #判断一个链表是否为回文结构# | 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;
    }
}

全部评论

相关推荐

买蜜雪也用卷:我觉得应该没有哪个人敢说自己熟练使用git,代码分支一复杂还是得慢慢寻思一下的,不过基本的拉代码提交代码还有分支什么的是应该会
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务