力扣细节题:回文链表

注意中间节点的寻找

复制代码
struct ListNode* reverseList(struct ListNode* head){
    if(!head) return NULL;
  struct ListNode*cur=head;
  struct ListNode*pre=NULL;
  struct ListNode*next;
    while(cur){
        next=cur->next;
        cur->next=pre;
        pre=cur;
        cur=next;
    
    }
    return pre;
}//将链表原地逆置

bool isPalindrome(struct ListNode* head){
    struct ListNode *p,*q;
    if(!head||!head->next) return true;
    //若单链表只有一个或为空则为回文链表
    p=head;
    q=head->next;
    while(q->next){
        p=p->next;
        q=q->next;
        if(q->next)
            q=q->next; 
    }
    q=p->next;  //此时p指向链表中点,q指向链表后半段的第一个结点
    q=reverseList(q);
    p=head;
    while(q){
        if(p->val!=q->val)
            return false;
        p=p->next;
        q=q->next;
    }
    return true;
}
相关推荐
过期动态8 小时前
【LeetCode 热题 100】移动零
java·数据结构·算法·leetcode·职场和发展·rabbitmq
菜菜的顾清寒10 小时前
力扣HOT100(32)二叉树的中序遍历
数据结构·算法·leetcode
csdn_aspnet12 小时前
java 算法 LeetCode 编号 70 - 爬楼梯
java·开发语言·算法·leetcode
_日拱一卒13 小时前
LeetCode:200岛屿数量
算法·leetcode·职场和发展
圣保罗的大教堂13 小时前
leetcode 153. 寻找旋转排序数组中的最小值 中等
leetcode
csdn_aspnet13 小时前
C语言 算法 LeetCode 编号 70 - 爬楼梯
c语言·开发语言·算法·leetcode
菜菜的顾清寒14 小时前
力扣HOT100(31)K 个一组翻转链表
算法·leetcode·链表
x_xbx14 小时前
LeetCode:101. 对称二叉树
算法·leetcode·职场和发展
风筝在晴天搁浅15 小时前
阿里 LeetCode 1189.“气球“的最大数量
算法·leetcode
木井巳15 小时前
【DFS解决floodfill算法】图像渲染
java·算法·leetcode·深度优先