判断链表是否为回文结构
/* * function ListNode(x){ * this.val = x; * this.next = null; * } */ /** * * @param head ListNode类 the head * @return bool布尔型 */ function isPail( head ) { if(!head) return true; var stack = []; var p = head; var s = head; while(p){ stack.push(p); p = p.next; } while(s){ var n = stack.pop(); if (s.val !== n.val) { return false; } s = s.next; } return true; } module.exports = { isPail : isPail };
链表算法 文章被收录于专栏
链表相关算法