题解 | #判断一个链表是否为回文结构#
判断一个链表是否为回文结构
https://www.nowcoder.com/practice/3fed228444e740c8be66232ce8b87c2f
bool isPail(struct ListNode* head ) {
// write code here
if(head==NULL || head->next==NULL)
return true;
struct ListNode *p=head,*q,*head2;
head2=(struct ListNode*)malloc(sizeof(struct ListNode));
head2->next=NULL;
while(p!=NULL)
{
q=(struct ListNode*)malloc(sizeof(struct ListNode));
q->val=p->val;
q->next=head2->next;
head2->next=q;
p=p->next;
}
p=head;
while(p!=NULL)
{
if(p->val!=q->val)
return false;
p=p->next;
q=q->next;
}
return true;
}
