稀碎从零算法笔记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;
    }
};

结算页面

时空复杂度都很高。。

相关推荐
宇钶宇夕18 分钟前
西门子 S7-200 SMART PLC 编程:转换 / 定时器 / 计数器指令详解 + 实战案例(指令讲解篇)
运维·算法·自动化
我叫汪枫18 分钟前
Spring Boot图片验证码功能实现详解 - 从零开始到完美运行
java·前端·javascript·css·算法·html
lifallen1 小时前
揭秘KafkaStreams 线程缓存:NamedCache深度解析
数据结构·算法·缓存·kafka·apache
我的知识太少了1 小时前
P1122 最大子树和
算法
郝学胜-神的一滴1 小时前
深入浅出 C++20:新特性与实践
开发语言·c++·程序人生·算法·c++20
Jelena技术达人1 小时前
淘宝/天猫按图搜索(拍立淘)item_search_img API接口实战指南
算法·图搜索算法
Adorable老犀牛1 小时前
阿里云-基于通义灵码实现高效 AI 编码 | 8 | 上手实操:LeetCode学习宝典,通义灵码赋能算法高效突破
学习·算法·leetcode
望获linux2 小时前
【实时Linux实战系列】规避缺页中断:mlock/hugetlb 与页面预热
java·linux·服务器·数据库·chrome·算法
菜就多练,以前是以前,现在是现在2 小时前
Codeforces Round 1048 (Div. 2)
数据结构·c++·算法
林木辛2 小时前
LeetCode 热题 160.相交链表(双指针)
算法·leetcode·链表