一个容易想到的方法:将链表的每个节点的val存入一个数组中,最后用双指针访问
有关双指针判断回文链表的思想参见
bool isPalindrome(struct ListNode* head)
{
int arr[100000]={0};
int i=0;
struct ListNode* cur=head;
while (cur)
{
arr[i]=cur->val;
i++;
cur=cur->next;
}
int* left=&arr[0];
int* right=&arr[i-1];
while (left<right)
{
if (*left==*right)
{
left++;
right--;
}
else
return false;
}
return true;
}
时间复杂度和空间复杂度均为
因篇幅问题不能全部显示,请点此查看更多更全内容