当前位置:网站首页>[palindrome structure of or36 linked list]

[palindrome structure of or36 linked list]

2022-06-22 21:06:00 Cat star people who love Durian


One 、 Title Description

 Insert picture description here
Topic link


Two 、 Provide easy to read code diagram

 Insert picture description here


3、 ... and 、 Title code

/* struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) {} };*/
struct ListNode *findMid(struct ListNode*head)
{
    
    struct ListNode* fast=head;
    struct ListNode* slow=head;
    while(fast&&fast->next)
    {
    
        slow=slow->next;
        fast=fast->next->next;
    }
    return slow;
}
struct ListNode * reserve(struct ListNode*head)
{
    
    struct ListNode*newHead=NULL;
    struct ListNode*cur=head;
    while(cur)
    {
    
        struct ListNode*next=cur->next;
        cur->next=newHead;
        newHead=cur;
        cur=next;
    }
    return newHead;
}
class PalindromeList {
    
public:
    bool chkPalindrome(ListNode* A) {
    
        ListNode* mid=findMid(A);
        ListNode*head=reserve(mid);
        ListNode*rhead=head;
        ListNode*Ahead=A;
        while(rhead&&Ahead)
        {
    
            if(rhead->val!=Ahead->val)
            {
    
                return false;
            }
            else
            {
    
                rhead=rhead->next;
                Ahead=Ahead->next;
            }
        }
        return true;
    }
};

The above is the whole content of this article , If there are mistakes in the article or something you don't understand , Communicate more with meow bloggers . Learn from each other and make progress . If this article helps you , Can give meow bloggers a concern , Your support is my biggest motivation .

原网站

版权声明
本文为[Cat star people who love Durian]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221941073862.html