将链表转换成数组存储,将数组转换成字符串,通过比较反转后的数组形成的字符串与原数组形成的字符串是否相等来判断数组是否为回文结构。
/*
* function ListNode(x){
* this.val = x;
* this.next = null;
* }
*/
/**
*
* @param head ListNode类 the head
* @return bool布尔型
*/
function isPail( head ) {
// write code here
if(!head || !head.next) return true
const res = []
let cur = head
while(cur){
res.push(cur.val)
cur = cur.next
}
return res.join("") === res.reverse().join("")
}
module.exports = {
isPail : isPail
};