题解 | #判断回文#

判断一个链表是否为回文结构

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;
    }
};
全部评论

相关推荐

04-30 15:51
已编辑
上海交通大学 机械工程师
点赞 评论 收藏
分享
谁知道呢_:要掉小珍珠了,库库学三年,这个结果
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务