Cover image for leetcode热题100 P234 回文链表

leetcode热题100 P234 回文链表

字数 424
阅读
访客

时间轴

时间轴

2026-03-13

init

反转链表 + 快慢指针

题目:

快慢指针把链表分为均等的两部分,注意链表节点为偶数和奇数有不同。然后把前面那部分链表反转。

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
/** * 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) {} * }; */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 false;                if (head->next == nullptr)                        return true;                ListNode *low = head, *fast = head, *left, *right;                int cnt = 1;                // 快慢指针                while (fast->next != nullptr) {                        if (low->next)                                low = low->next;                        if (fast->next) {                                fast = fast->next;                                cnt++;                        }                        if (fast->next) {                                fast = fast->next;                                cnt++;                        }                }                // 1 2 3 1 low->3 fast->1                // 1 2 1   low->2 fast->1                // split to [head, low), [low, fast](偶数, 奇数要舍弃low)                // revserse [head, low)                ListNode *prev = head, *p = head->next;                ListNode *next;                while (p && p != low) {                        next = p->next; // store the next                        p->next = prev;                        prev = p;                        p = next; // restore the next                }                head->next = nullptr;                left = prev;                if (cnt % 2 == 0)                        right = low;                else                        right = low->next;                // 比较                while (left && right && left->val == right->val) {                        left = left->next;                        right = right->next;                }                if (left == nullptr && right == nullptr)                        return true;                return false;        }};#include <vector>using std::vector;int main(){        vector<int> vec = { 1, 2, 3, 4 };        ListNode *head = nullptr, *tail = nullptr;        for (int val : vec) { // 尾插法                if (head == nullptr && tail == nullptr) {                        head = tail = new ListNode(val);                } else {                        tail->next = new ListNode(val);                        tail = tail->next;                }        }        Solution S;        S.isPalindrome(head);}
评论加载中…