利用栈结构来判断链表是否为回文结构。
先保存链表节点的数据,然后再比较数据
/* * function ListNode(x){ * this.val = x; * this.next = null; * } */ /** * * @param head ListNode类 the head * @return bool布尔型 */ function isPail( head ) { // write code here let temp = head; let stack = [] while(temp){ stack.push(temp.val); temp = temp.next; } while(head){ if(head.val!=stack.pop()){ return false; } head = head.next; } return true; } module.exports = { isPail : isPail };