题解 | #判断一个链表是否为回文结构#

判断一个链表是否为回文结构

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;
}

全部评论

相关推荐

评论
5
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务