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

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

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

全部评论

相关推荐

用户64975461947315:这不很正常吗,2个月开实习证明,这个薪资也还算合理,深圳Java好多150不包吃不包住呢,而且也提前和你说了没有转正机会,现在贼多牛马公司骗你说毕业转正,你辛辛苦苦干了半年拿到毕业证,后面和你说没hc了😂
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
04-25 10:45
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务