目录
题目叙述
题解代码
class Solution {
public:
bool isPalindrome(ListNode* head) {
if(head == nullptr)
return true;
//通过构建数组将链表中结点的值复制到数组中,利用数组来判断其是否回文
vector<int>vals;
while(head != nullptr)
{
vals.push_back(head->val);
head = head->next;
}
for(int i = 0, j = (int)vals.size()-1; i < j; i++, j--)
{
if(vals[i] != vals[j])
{
return false;
}
}
return true;
}
};
提交结果