稀碎从零算法笔记Day20-LeetCode:回文链表

题型:链表、双指针

链接:206. 反转链表 - 力扣(LeetCode) 234. 回文链表 - 力扣(LeetCode)

来源:LeetCode

题目描述(红字为笔者添加)

给你一个单链表的头节点 head ,请你判断该链表是否为回文链表。如果是,返回 true ;否则,返回 false

回文串Ver.链表

题目样例

示例 1:

复制代码
输入:head = [1,2,2,1]
输出:true

示例 2:

复制代码
输入:head = [1,2]
输出:false

提示:

  • 链表中节点数目在范围[1, 105]
  • 0 <= Node.val <= 9

题目思路

回文串就是【正着/反着读一样】的串。

题目可拆分为:①反转字符串 ②判断回文串

笔者思路:将链表映射到 链表/数组。链表映射到链表:链表 因为有地址的原因,映射到另一个链表即new一个新结点 保存val。考虑到结构体这个蛋疼的内存以及实现难度,笔者建议直接映射到数组为优解

判断回文串这里笔者推荐双指针(两个指针双向奔赴,相遇的时候就说明是回文串)

当然,你可以直接用STL中的reverse /笑

C++代码

映射到另一个链表:(真是壮观)

cpp 复制代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    bool isPalindrome(ListNode* head) {
        // 用一个新的链表来存,然后链表相同的话就是
        ListNode* temp2 = new ListNode(0);
        ListNode* temp1 = head;
        // ListNode* p;
        while (temp1 != NULL) {
            // p = temp1;
            ListNode *temp = new ListNode(0);
            temp->val = temp1->val;
            temp1 = temp1->next;
            temp->next = temp2->next;
            temp2->next = temp;
        }
        ListNode* real = temp2->next;
        temp1 = head;
        while (temp1 != NULL && real != NULL) {
            //cout << temp1->val << " " << real->val;
            if (temp1->val != real->val)
                return 0;
            
            temp1 = temp1->next;
            real = real->next;
            
        }
        delete temp2;
        return 1;
    }
};

用了reverse(懒狗!)

cpp 复制代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    bool isPalindrome(ListNode* head) {
        // 数值映射到数组
        // 字符串判断回文串
        vector<int> num;
        while(head != NULL)
        {
            num.push_back(head->val);
            head = head->next;
        }
        vector<int> temp = num;
        reverse(num.begin(),num.end());
        for(int i=0;i<num.size();i++)
        {
            if(num[i] != temp[i])
                return 0;
        }
        //string str = to_string(num);
        return 1;

    }
};

双指针

cpp 复制代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    bool isPalindrome(ListNode* head) {
        //链表映射到数组 
          vector<int> num;
        while(head != NULL)
        {
            num.push_back(head->val);
            head = head->next;
        }
        int left=0,right=num.size()-1;
        while(left < right)
        {
            if(num[left] != num[right])
                return 0;
            left++;
            right--;
        }
        return 1;
    }
};

结算页面

时空复杂度都很高。。

相关推荐
坚持编程的菜鸟7 小时前
模拟实现memmove
c语言·算法·模拟实现memmove
wabs6668 小时前
关于图论【最短路径之Dijkstra算法(堆优化版)|卡码网47.参加科学大会的思考】
数据结构·算法·图论·优先级队列·邻接表·小顶堆·卡码网
果果燕8 小时前
实习笔记(一):NFS、Samba、VNC、Git、CMake、systemd、内核、交叉编译
linux·arm开发·c++·笔记·git
坚持编程的菜鸟8 小时前
编写判断大小端程序
c语言·算法·判断大小端
papaofdoudou8 小时前
判断排列逆序奇偶性的乘积判别法(范德蒙德符号法)
人工智能·算法
linux-hzh9 小时前
百日算法修炼 · Day 03
java·算法
问商十三载9 小时前
RAG 检索效果差怎么排查?2026 五层诊断法完整指南
开发语言·人工智能·windows·python·算法
嵌入式老牛10 小时前
三相电气量采集模块设计(三)非同步采样时的精度提升
算法·精度·计量
Zguigo10 小时前
第25-26讲:计算机操作系统存储管理——分页机制、页表与快表(TLB)
linux·windows·笔记
爱音乐的marlon11 小时前
FastAPI 学习笔记03|core 核心层:config 是总控制台,database 是整个工程的地基
笔记·学习·fastapi