题解 | #判断一个链表是否为回文结构#

/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 * };
 */

class Solution {
public:
    /**
     * 
     * @param head ListNode类 the head
     * @return bool布尔型
     */
    bool isPail(ListNode* head) {
        // write code here
        vector<int> t;
        int i = 0;
        while(head != nullptr){
            t.push_back(head->val);
            head = head->next;
            i++;
        }
        int k = i/2;
        for(int j=0;j<k;j++){
            if(t[j] != t[i-1-j]) return false;
        }
        return true;
    }
};

全部评论

相关推荐

点赞 评论 收藏
转发
1 收藏 评论
分享
牛客网
牛客企业服务