力扣细节题:回文链表

注意中间节点的寻找

复制代码
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;
}
相关推荐
rannn_11128 分钟前
【力扣hot100】哈希表专题——从两数之和到最长连续序列
java·算法·leetcode·哈希
go不是csgo2 小时前
从模板到五道 LeetCode:完全背包的 Golang 教学笔记
笔记·leetcode·golang
keep intensify18 小时前
最长有效括号
算法·leetcode·动态规划
CoderYanger18 小时前
A.每日一题:1979. 找出数组的最大公约数
java·程序人生·算法·leetcode·面试·职场和发展·学习方法
INGNIGHT1 天前
528.按权重随机选择(前缀和&二分法)加权负载均衡
算法·leetcode
runafterhit1 天前
python基础语法命令(C程序员刷leetcode)
c语言·python·leetcode
青山木1 天前
Hot 100 --- 全排列
java·数据结构·算法·leetcode·深度优先
玖玥拾1 天前
LeetCode 13 罗马数字转整数
笔记·算法·leetcode
闪电悠米2 天前
力扣hot100-48.旋转图像-转置翻转详解
算法·leetcode·职场和发展
啦啦啦啦啦zzzz2 天前
算法:贪心算法
c++·算法·leetcode·贪心算法