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

结算页面

时空复杂度都很高。。

相关推荐
海海不掉头发29 分钟前
【计算机组成原理】-CPU章节学习篇—笔记随笔
笔记·单片机·学习·考研·计算机组成原理
用户50408278583939 分钟前
1. RAG 权威指南:从本地实现到生产级优化的全面实践
算法
Python×CATIA工业智造2 小时前
详细页智能解析算法:洞悉海量页面数据的核心技术
爬虫·算法·pycharm
Swift社区2 小时前
Swift 解 LeetCode 321:拼接两个数组中的最大数,贪心 + 合并全解析
开发语言·leetcode·swift
岑梓铭3 小时前
计算机网络第九章——数据链路层《局域网》
网络·笔记·计算机网络·考研·408
无聊的小坏坏3 小时前
力扣 239 题:滑动窗口最大值的两种高效解法
c++·算法·leetcode
黎明smaly3 小时前
【排序】插入排序
c语言·开发语言·数据结构·c++·算法·排序算法
njsgcs3 小时前
cad_recognition 笔记
笔记
YuTaoShao3 小时前
【LeetCode 热题 100】206. 反转链表——(解法一)值翻转
算法·leetcode·链表
YuTaoShao3 小时前
【LeetCode 热题 100】142. 环形链表 II——快慢指针
java·算法·leetcode·链表