判断一个链表是否为回文结构_牛客题霸_牛客网 (nowcoder.com)
1. 利用一个vector容器 将链表中元素依次放入
2. 双指针依次
/** * 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> res; while(head != nullptr) { res.push_back(head->val); head = head->next; } int left = 0; int right = res.size()-1; while(left < right) { if(res[left] != res[right]) return false; left++; right--; } return true; } };
判断 头尾元素是否相等