题解 | #链表的回文结构#
链表的回文结构
https://www.nowcoder.com/practice/d281619e4b3e4a60a2cc66ea32855bfa
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};*/
class PalindromeList {
public:
bool chkPalindrome(ListNode* A)
{
//思路1:额外开辟一个数组,把链表拷贝到数组去,再利用左右下标对比是否相等
int arr[900] = {0};
int i = 0;
while(A)
{
arr[i] = A->val;
i++;
A = A->next;
}
int left = 0;
int right = i-1;
while(left <= right)
{
if(arr[left] == arr[right])
{
left++;
right--;
}
else
{
return false;
}
}
return true;
}
};
查看14道真题和解析