'Why am I getting this error member access within null pointer of type 'ListNode'

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:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        int count=0;
        ListNode* temp2 = head; 
        ListNode* temp1 = head; 
        ListNode* temp = head; 
        while (temp != NULL) 
        { 
          ++count; 
          temp = temp->next; 
        }
        
        
        int i=1;
        
        while(i!=(n-count) && temp1->next!=NULL && temp2->next!=NULL)
        {
            i++;
            temp2=temp2->next;
            temp1=temp1->next;
        }
       
        temp2=temp1->next;      //move temp2 to next
        
        temp2->val=temp1->val;   //put val of temp2 to temp---so val erased
        
        temp1->next=temp2->next;    //adjust link
        
        delete(temp2);      //free
        
        return head;
        
    }
};

I got this error

member access within null pointer of type 'struct ListNode'

I did apply null checks, but I'm not able to detect the error. It's about "Remove Nth Node From End of List".



Solution 1:[1]

You need to rethink or clearly define what "nth" means.

What about the corner cases? Like -1 or 0 or count-1 or count or count+1.

After that, please add some validation of the parameter "n".

You have also one major problem in the code. You mixed up (count - n) with 0(n - count)

A potential solution with assumptions on what nth means could be:


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:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        int count = 0;
        ListNode* temp2 = head;
        ListNode* temp1 = head;
        ListNode* temp = head;
        while (temp != nullptr)
        {
            ++count;
            temp = temp->next;
        }
        if ((count > 0) && (n>0) && ((count - n) > 0)) {
            int i = 1;

            while (i != (count - n) && temp1->next != nullptr && temp2->next != nullptr)
            {
                i++;
                temp2 = temp2->next;
                temp1 = temp1->next;
            }
            temp2 = temp1->next;      //move temp2 to next

            //temp2->val = temp1->val;   //put val of temp2 to temp---so val erased

            temp1->next = temp2->next;    //adjust link

            delete temp2;      //free

            return head;
        }
    }
};
int main() {

    // Build linked list
    ListNode *n5 = new ListNode(5);
    ListNode* n4 = new ListNode(4, n5);
    ListNode* n3 = new ListNode(3, n4);
    ListNode* n2 = new ListNode(2, n3);
    ListNode* n1 = new ListNode(1, n2);
    ListNode* n0 = new ListNode(0, n1);

    Solution solution;
    solution.removeNthFromEnd(n0,6);
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Armin Montigny