力扣细节题:回文链表

注意中间节点的寻找

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

相关推荐

  1. 】234.

    2024-03-10 01:38:05       49 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-10 01:38:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-10 01:38:05       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-10 01:38:05       82 阅读
  4. Python语言-面向对象

    2024-03-10 01:38:05       91 阅读

热门阅读

  1. python脚本批量关闭exe文件

    2024-03-10 01:38:05       39 阅读
  2. 用 reduce 实现 map 的功能

    2024-03-10 01:38:05       48 阅读
  3. 【C#语言入门】13. 表达式、语句详解(3)

    2024-03-10 01:38:05       50 阅读
  4. 基于单片机的输液监测系统设计与实现

    2024-03-10 01:38:05       41 阅读
  5. 鸿蒙崛起:能否颠覆安卓霸主地位?

    2024-03-10 01:38:05       47 阅读
  6. mongodb的备份与恢复

    2024-03-10 01:38:05       43 阅读
  7. python中的模块和包

    2024-03-10 01:38:05       50 阅读
  8. el-aside中添加el-menu设置collapse宽度自适应

    2024-03-10 01:38:05       41 阅读