Sakura

sakura

博客园 首页 新随笔 联系 订阅 管理

#define Node ListNode

class Solution {
public:
    /**
     * 
     * @param head ListNode类 the head
     * @return bool布尔型
     */
    bool isPail(ListNode* head) {
        if(head==NULL ||head->next ==NULL) return true;
        // write code here
        ListNode*first=head,*last=head;
        while(first && first->next) first=first->next->next,last=last->next;
        stack<Node*>s;
        while(last) {
            s.push(last);
            last = last->next;
        }
        last = head;
        while(s.size() && last->val == s.top()->val) last = last->next,s.pop();
        return s.empty();
        
    }
};

posted on 2021-01-24 21:23  .geek  阅读(53)  评论(0编辑  收藏  举报