回文链表
思路:找到链表的中间节点然后前面一部分进行逆置,变成两个链表,然后将值逐个进行比较
样例
Input: 1->2->2->1
Output: true
class Solution {
public:
bool isPalindrome(ListNode* head) {
int lenth, i;
ListNode *point1, *point2, *point3;
point3 = point2 = head;
point1 = NULL;
lenth = 0;
if(head == NULL || head->next == NULL)
return true;
while(point3 != NULL)//取得长度
{
point3 = point3->next;
lenth++;
}
for(i = 0;i < (lenth / 2);i++)//遍历到中间,并逆置
{
point3 = point2->next;
point2->next = point1;
point1 = point2;
point2 = point3;
}
if((lenth % 2) == 1)
point3 = point3->next;
while(point3 !=