刷题-Leetcode-234. 回文链表

234. 回文链表

题目链接

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/palindrome-linked-list/
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

题目描述

请判断一个链表是否为回文链表。

示例 1:

输入: 1->2
输出: false
示例 2:

输入: 1->2->2->1
输出: true
进阶:
你能否用 O(n) 时间复杂度和 O(1) 空间复杂度解决此题?

题目分析

做完力扣206再来做这题
1.双指针(数组)空间o(n)-链表的数字放进数组里 第一个指针在头,第二个指针在尾

/**
 * 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> nums;
        ListNode* dummy = head;
        while(dummy){
    
    
            nums.push_back(dummy->val);
            dummy = dummy->next;
        }
        int n = nums.size();
        int left = 0;
        int right = n - 1;
        while(left < right){
    
    
            if(nums[left] != nums[right]){
    
    
                return false;
            }
            left++;
            right--;
        }
        return true;
    }
};

2.递归+双指针 递归栈空间o(n)

/**
 * 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:
    ListNode * global;
    bool isPalindrome(ListNode* head) {
    
    
        global = head;
        return recursion(head);

    }
    bool recursion(ListNode* head){
    
    
        if(!head){
    
    
            return true;
        }
        if(!recursion(head->next)){
    
    
            return false;
        }
        if(global->val != head->val){
    
    
            return false;
        }
        global = global->next;//注意 别忘了
        return true;
    }
};

3.栈-空间o(n)栈底对应head 栈尾对应tail 从链表head 开始 与 栈顶判断

/**
 * 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) {
    
    
        stack<ListNode*> stack;//栈内存储指针
        ListNode * dummy = head;
        while(dummy){
    
    
            stack.push(dummy);
            dummy = dummy->next;
        }
        dummy = head;
        while(dummy){
    
    
            if(dummy->val != stack.top()->val){
    
    
                return false;
            }
            dummy = dummy->next;
            stack.pop();
        }
        return true;
    }
};

进阶
4.快慢指针(链表常用)-空间o(1)
慢指针走一步 快指针走两步 快指针到nullptr的时候 慢指针到链表后一半开始的位置 快指针指向head 将慢指针及慢指针后面反转(反转链表 题206))
快慢指针val相等同时移动 不等返回false
https://leetcode-cn.com/problems/palindrome-linked-list/solution/wo-de-kuai-man-zhi-zhen-du-cong-tou-kai-shi-gan-ju/

猜你喜欢

转载自blog.csdn.net/qq_42771487/article/details/113993130