题解 | #判断回文#
判断一个链表是否为回文结构
http://www.nowcoder.com/practice/3fed228444e740c8be66232ce8b87c2f
/** * struct ListNode { * int val; * struct ListNode *next; * }; */ class Solution { public: /** * * @param head ListNode类 the head * @return bool布尔型 */ bool isPail(ListNode* head) { // write code here if (!head || !head->next) { return true; } stack<ListNode *> stack; ListNode* node = head; while (node) { stack.push(node); node = node->next; } ListNode* cur = head; while (!stack.empty()) { if (cur->val != stack.top()->val) { return false; } cur = cur->next; stack.pop(); } return true; } };