《LeetCode之每日一题》:155.回文链表

Posted 是七喜呀!

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了《LeetCode之每日一题》:155.回文链表相关的知识,希望对你有一定的参考价值。

回文链表


题目链接: 回文链表

有关题目


提示:

链表中节点数目在范围[1, 10^5]0 <= Node.val <= 9
进阶:你能否用 O(n) 时间复杂度和 O(1) 空间复杂度解决此题?

题解

法一:数组 + 双指针

/**
 * 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) {
        int cnt = 0;
        vector<int> a;
        while(head != nullptr){
            a.push_back(head->val);//a.emplace_back(head->val)
            head = head->next;
        }
        int l = 0, r = a.size() - 1;
        while(l < r){
            if (a[l++] != a[r--]){
                return false;
            }
        }
        return true;
    }
};


法二:递归
参考官方题解

/**
 * 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 {
    ListNode* frontPointer;

public:
    bool recursivelyCheck(ListNode* currentNode){
        if (currentNode != nullptr){
            if (!recursivelyCheck(currentNode->next)) return false;
            if (frontPointer->val != currentNode->val) return false;
            frontPointer = frontPointer->next;
        }
        return true;
    }
    bool isPalindrome(ListNode* head) {
        frontPointer = head;
        return recursivelyCheck(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) {

        //找到前半部分的尾节点 并反转后半部分链表
        ListNode* firstHalfEnd = endOfFirstHalf(head);
        ListNode* secondHalfStart = reverseList(firstHalfEnd->next);

        //判断是否为回文
        ListNode* p1 = head;
        ListNode* p2 = secondHalfStart;
        bool res = true;
        while(res && p2 != nullptr){//注意p2 结束条件为 p2 != nullptr
            if (p1->val != p2->val){
                res = false;
            }
            p1 = p1->next, p2 = p2->next;
        }

        //还原原链表并返回结果
        firstHalfEnd->next = reverseList(secondHalfStart);
        return res;
    }

    ListNode* reverseList(ListNode* head){
        ListNode* pre = nullptr;
        ListNode* cur = head; 
        while(cur != nullptr){
            ListNode* temp = cur->next;
            cur->next = pre;
            pre = cur;
            cur = temp;
        }
        return pre;
    }

    ListNode* endOfFirstHalf(ListNode* head){
        ListNode* first = head;
        ListNode* second = head;
        while(first->next != nullptr && first->next->next != nullptr){
            first = first->next->next;
            second = second->next;
        }
        return second;
    }
};

以上是关于《LeetCode之每日一题》:155.回文链表的主要内容,如果未能解决你的问题,请参考以下文章

Java每日一题——>剑指 Offer II 027. 回文链表

Java每日一题——>剑指 Offer II 027. 回文链表

《LeetCode之每日一题》:132.验证回文串

《LeetCode之每日一题》:246.分割回文串

《LeetCode之每日一题》:65.最长回文子串

《LeetCode之每日一题》:286.移除链表元素