单链表算法 - 链表的回文结构

链表的回文结构_牛客题霸_牛客网对于一个链表,请设计一个时间复杂度为O(n),额外空间复杂度为O(1)的算法,判断其是否为。题目来自【牛客题霸】icon-default.png?t=N7T8https://www.nowcoder.com/practice/d281619e4b3e4a60a2cc66ea32855bfa思路1:

/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};*/
class PalindromeList {
public:
    bool chkPalindrome(ListNode* A) {
        int left = 0;
        int right = 0;
        // write code here
        //创建数组
        int count = 0;
        //创建数组
        int arr[900];
        ListNode* ptail = A;
        //将链表中的数据插入到数组里面以及计算链表的个数
        while(A)
        {
            arr[count++] = A->val;
            A = A->next;
        }
        //下标是从0开始的
        right = count-1;
        //比较是否为回文结构
        while(left++ < right--)
        {
            if(arr[left] != arr[right])
            {
                return false;
            }
        }
        return true;
    }
};

提交结果:

在代码中我们申请了900个整型的数组,因为我们知道链表最大是900个节点,这里的空间复杂度也是O(1),复合要求,那如果这里没有对链表节点个数进行限制,这种思路肯定是不行的。

除此之外,该思路只能在牛客上通过,若换成力扣,通过不了,因为如果明确规定空间复杂度为O(1)的话就不能额外的去申请空间,更别说申请900个整型了,牛客平台对时间和空间复杂度没有力扣那么严格。

思路2:

代码: 

/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};*/

//C++和C在这里唯一的区别就是不需要再typedef
//typedef struct ListNode ListNode; - 不用


class PalindromeList {
public:
ListNode* reveres(ListNode* A)
{
    ListNode* prev = NULL;
    ListNode* ptail = A;
    ListNode* next = A->next;
    while(ptail)
    {
        ptail->next = prev;
        prev = ptail;
        ptail = next;
        next = next->next;
    }
    return prev;
}
    bool chkPalindrome(ListNode* A) {
        // write code here
        //找中间节点
        ListNode* fast = A;
        ListNode* slow = A;
        while(slow && slow->next)
        {
            fast = fast->next;
            slow = slow->next->next;
        }
        //fast就是中间节点
        ListNode* right =  reveres(fast);//逆置并返回逆置后的第一个节点
        ListNode* left = A;
        while(right)
        {
            if(right->val != left->val)
            {
                return false;
            }
            right = right->next;
            left = left->next;
        }
        return true;
    }
};

提交结果:

相关推荐

  1. (对称)结构

    2024-07-17 06:16:05       33 阅读

最近更新

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

    2024-07-17 06:16:05       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-17 06:16:05       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-17 06:16:05       57 阅读
  4. Python语言-面向对象

    2024-07-17 06:16:05       68 阅读

热门阅读

  1. 网络编程part3

    2024-07-17 06:16:05       21 阅读
  2. linux-arm ubuntu18.04 qmqtt5.12.6 编译部署

    2024-07-17 06:16:05       24 阅读
  3. go面试题 Day3

    2024-07-17 06:16:05       24 阅读
  4. MySQL零散拾遗(二)

    2024-07-17 06:16:05       24 阅读
  5. chrome扩展清除指定站点缓存chrome.browsingData.remove

    2024-07-17 06:16:05       28 阅读
  6. linux中导出sql脚本

    2024-07-17 06:16:05       21 阅读
  7. git 提交远程仓库 方式

    2024-07-17 06:16:05       27 阅读
  8. 热修复的原理

    2024-07-17 06:16:05       22 阅读
  9. Springboot 3.x - Reactive programming (2)

    2024-07-17 06:16:05       25 阅读
  10. C++基础语法:STL之容器(1)--容器概述和序列概述

    2024-07-17 06:16:05       31 阅读
  11. 【前端】原生实现图片的放大与缩放

    2024-07-17 06:16:05       22 阅读