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

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

https://www.nowcoder.com/practice/3fed228444e740c8be66232ce8b87c2f

单链表,第一反应栈,懒得写栈,递归

/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 * };
 */
/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param head ListNode类 the head
 * @return bool布尔型
 */

struct ListNode* pail(struct ListNode *this,struct ListNode *head){
    struct ListNode* tmp;
    if(this->next!=NULL){
        tmp = pail(this->next,head);
    }else{
        tmp = head;
    }
    if(tmp==NULL){
        return NULL;
    }
    if(tmp->val == this->val){
        if(tmp->next!=NULL){
            return tmp->next;
        }return head;
    }
    return NULL;
}

bool isPail(struct ListNode* head ) {
    // write code here
    if(head == NULL){
        return true;
    }
    struct ListNode* tmp =  pail(head,head);
    if(tmp==NULL){
        return false;
    }return true;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务