Skip to content

Latest commit

 

History

History
27 lines (22 loc) · 845 Bytes

234.palindrome-linked-list.md

File metadata and controls

27 lines (22 loc) · 845 Bytes

問題

https://leetcode.com/problems/palindrome-linked-list/

回答

初回の回答: Vec に値を入れて前半部分と後半部分を比較する

C 言語を勉強していたときに連結リストの存在は知っていたので、なんとなく連結リストの中身のとり方はイメージできていた。 reverse()メソッドは返り値がほしいなと思った。

impl Solution {
    pub fn is_palindrome(head: Option<Box<ListNode>>) -> bool {
        let mut current: Option<Box<ListNode>> = head;
        let mut vec: Vec<i32> = Vec::new();
        while let Some(list) = current {
            current = list.next;
            vec.push(list.val);
        }
        let len = vec.len() / 2;
        let mut vec2 = vec.split_off(len);
        vec2.reverse();
        vec == vec2[..len]
    }
}