leetcode系列–第234题.回文链表
给你一个单链表的头节点 head ,请你判断该链表是否为回文链表。如果是,返回 true ;否则,返回 false 。
输入:head = [1,2,2,1]
输出:true
// 作为一个菜🐔前端,leetcode中最不太喜欢的题就是链表和树了,因为太抽象了,哈哈哈
// 先把链表的val存入数组中转换成判断数组是否是回文数组
var isPalindrome = function (head) {
const vals = []
while (head != null) {
vals.push(head.val)
head = head.next
}
let length = vals.length;
let l = 0,
r = length - 1;
while (l < r) {
if (vals[l] != vals[r]) {
return false
}
l += 1
r -= 1
}
return true
};