Search code examples
cruntime-errorsingly-linked-listpalindromenon-recursive

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


I am trying to solve isPalindrome() question on LeetCode using non recurisve solution, When i run this code using VSCode it runs and gives me the right output, but when i run it in LeetCode compiler it gives me the error mentioned below.

Can you help me solve this problem ? and is there's any modifications on my code may help me solve the problem in a better way ?

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]


Solution

  • your solution idea isn't wrong, but is too hard to implement and don't have any bug, I suggest my solution or suggest to write one method which will return reverse list and then just compare these two.

    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;
    }
    

    this solution I just came up, should be right - it just compares each node from left and from right, but I think this is not time which will change a lot. EDIT:

    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;
    }