满意答案
suweichun4
2017.03.17
采纳率:50% 等级:10
已帮助:773人
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
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
{
point3 = point2->next;
point2->next = point1;
point1 = point2;
point2 = point3;
}
if((lenth % 2) == 1)
point3 = point3->next;
while(point3 != NULL && point1 != NULL)//两个指针开始向两头移动,取值比较
{
if(point3->val != point1->val)
return false;
point3 = point3->next;
point1 = point1->next;
}
return true;//比较中没有发现不同值,则为回文链表
}
};
01分享举报