题解 | #判断一个链表是否为回文结构#
判断一个链表是否为回文结构
https://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 //创建一个数组容器,按顺序盛放链表中的元素 vector<int> t; //i表示数组长度 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; } };