思路:
找中间节点–>逆置->比较
代码:
/*** Definition for singly-linked list.* struct ListNode {* int val;* struct ListNode *next;* };*/struct ListNode* middleNode(struct ListNode* head)
{
struct ListNode*slow=head;
struct ListNode*flast=head;
while(flast&&flast->next)
{slow=slow->next;flast=flast->next->next;
}
return slow;
}
struct ListNode* reverseList(struct ListNode* head){struct ListNode*newhead=NULL;struct ListNode*cur=head;while(cur){struct ListNode*per=cur->next;cur->next=newhead;newhead=cur;cur=per;}return newhead;
}
bool isPalindrome(struct ListNode* head){
struct ListNode* mid=middleNode(head);//找中间节点struct ListNode* rmid=reverseList(mid);//逆置//比较while(head&&rmid){if(head->val==rmid->val){head=head->next;rmid=rmid->next;}else {return false;}}return true;
}