题解 | #判断一个链表是否为回文结构#
判断一个链表是否为回文结构
https://www.nowcoder.com/practice/3fed228444e740c8be66232ce8b87c2f
#include<vector>
class Solution {
public:
bool isPail(ListNode* head) {
if(head->next==nullptr||head==nullptr) return true;
auto a=head;
vector<int>b;
int c=0;
while(a)
{
b.push_back(a->val);
a=a->next;
c++;
}
for(;c>0;--c)
{
if(b[c-1]!=head->val)
{
return false;
}
head=head->next;
}
return true;
}
};
之前那个号由于 电话卡错误所以重开了一个(麻了),如果是用纯链表知识,我可能比较难去解决这个题,但我感觉一步步熟悉使用vector和queue后会比较轻松解决链表
查看25道真题和解析