类型空指针内的成员访问,C 编程是 Palindrome

member access within null pointer of type, C programming is Palindrome

我正在尝试使用非递归解决方案解决 LeetCode 上的 isPalindrome() 问题,当我 运行 这段代码使用 VSCode 时 运行s 并给我正确的输出,但是当我在 LeetCode 编译器中 运行 它给了我下面提到的错误。

你能帮我解决这个问题吗?我的代码是否有任何修改可以帮助我以更好的方式解决问题?

bool isPalindrome(struct ListNode* head){
    struct ListNode * NewHead, *MidList, *EndList;
    NewHead = EndList = MidList = head;

    struct ListNode *ptr_SecondHalf, *ptr_FirstHalf;
    while (EndList->next  != NULL)
    {
        EndList = EndList->next->next;
 
        MidList = MidList->next;

        if (EndList->next == NULL || EndList->next->next  == NULL){
                //ODD
            if (EndList->next == NULL){
                ptr_SecondHalf = MidList->next;
                break;
            }
            //EVEN
            if (EndList->next->next  == NULL){
                ptr_SecondHalf = MidList->next;
                break;
            } 
        }
                
    }


    MidList->next = NULL;

    ptr_FirstHalf = head;

    // Reverse SecondHalf
    struct ListNode* ptr_SecondHalf_Reversed = NULL;
    struct ListNode* current = ptr_SecondHalf;
    struct ListNode* next = NULL;
    
    while (current != NULL) {
        // Store next
        next = current->next;
 
        // Reverse current node's pointer
        current->next = ptr_SecondHalf_Reversed;
 
        // Move pointers one position ahead.
        ptr_SecondHalf_Reversed = current;
        current = next;
    }
    
    while (ptr_FirstHalf->next != NULL && ptr_SecondHalf_Reversed != NULL){
        
        if (ptr_FirstHalf->val == ptr_SecondHalf_Reversed->val){
            if (ptr_FirstHalf->next->next != NULL || ptr_SecondHalf_Reversed ->next != NULL){
                ptr_FirstHalf = ptr_FirstHalf->next;
                ptr_SecondHalf_Reversed = ptr_SecondHalf_Reversed ->next ;
            }
            else
            {
                break;
            }
            
        }else{
            printf("false \n");
            return false;
        }
    }
    printf("true \n");
    return true;
}
    

Line 12: Char 21: runtime error: member access within null pointer of type 'struct ListNode' [solution.c]

你的解决思路没有错,但是实现起来太难了,没有任何bug,我建议我的解决方案或者建议写一个方法return反转列表然后比较这两个。

bool isPalindromeRec(struct ListNode* left, struct ListNode* right) {
    if(right == NULL) return true;

    if(isPalindromeRec(left, right->next) && left->val == right->val) {
        left = left->next;
        return true;
    }
    return false;
}

bool isPalindrome(struct ListNode* head){
    if(head == NULL) return true;

    struct ListNode * left = head;
    struct ListNode * right = head;
    bool ans = isPalindromeRec(left, right);
    // happy now Vlad from Moscow?
    if(ans) printf("true\n");
    else printf("false\n"); 
    return ans;
}

我刚刚想到的这个解决方案,应该是正确的——它只是从左到右比较每个节点,但我认为这不是时间会改变很多。 编辑:

bool isPalindrome(struct ListNode *head) {
 
    if(head == NULL|| head->next== NULL)
        return true;
 
    //find list center
    struct ListNode *fast = head;
    struct ListNode *slow = head;
 
    while(fast->next != NULL && fast->next->next != NULL){
        fast = fast->next->next;
        slow = slow->next;
    }
 
    struct ListNode *secondHead = slow->next;
    slow->next = NULL;
 
    //reverse second part of the list
    struct ListNode *p1 = secondHead;
    struct ListNode *p2 = p1->next;
 
    while(p1 != NULL && p2 != NULL){
        struct ListNode *temp = p2->next;
        p2->next = p1;
        p1 = p2;
        p2 = temp;
    }
 
    secondHead->next = NULL;
 
    //compare two sublists now
    struct ListNode *p = (p2 == NULL?p1:p2);
    struct ListNode *q = head;
    while(p!=NULL){
        if(p->val != q->val)
            return false;
 
        p = p->next;
        q = q->next;
 
    }
 
    return true;
}