力扣 234. 回文链表

120 阅读1分钟

🔗 leetcode.cn/problems/pa…

题目

  • RT

思路

  • 实现了进阶版本,先遍历找到中间节点,反转单链表后,判断是否为回文

代码

/**
 * 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) {
        if (head == nullptr) return true;
        int count = 0;
        auto* curr = head;
        while (curr) {
            count++;
            curr = curr->next;
        }
        int index = count / 2 + 1;
        count = 0;
        curr = head;
        while (curr) {
            count++;
            if (count == index) break;
            curr = curr->next;
        }

        ListNode* pre = nullptr;
        while (curr->next) {
            auto* next = curr->next;
            curr->next = pre;
            pre = curr;
            curr = next;
        }
        curr->next = pre;

        while (curr && head) {
            if (curr->val != head->val) return false;
            curr = curr->next;
            head = head->next;
        }
        return true;
 
    }
};