问题
I am trying to solve a practical coding question regarding linked lists where I am supposed to add the value in each respective node to form a new linked list. However I'm getting this error: Line 13: Char 20: runtime error: member access within null pointer of type 'struct ListNode' (solution.cpp)
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode* nodes;
nodes->val = l1->val + l2->val;
nodes->next->val = l1->next->val + l2->next->val;
nodes->next->next->val = l1->next->next->val + l2->next->next->val;
return nodes;
}
};
回答1:
You have to allocate memory for your nodes
Variable first.
ListNode* nodes = new ListNode();
But don't forget to delete it if you are not using the variable anymore or else you'll get memory leaks.
回答2:
The result nodes is not allocated.
You are accessing 6 pointers without making sure any of them are non-null. You need to check for nullity of
l1, l1->next, l1->next->next
l2, l2->next, l2->next->next
addTwoNumbers actually add 6 numbers. This can't be right. Either addTwoNumbers
add at most two numbers, or call your method addTwoLists
Please remember that your linked list has an end, so this code is guaranteed to break for the last two elements.
You need to rethink the complete method.
回答3:
As spotted already in comments and answers, you do not allocate memory for the new nodes.
But there are a few other issues, too. You seem to assume that both lists contain exactly three nodes. What makes you so sure about? What if one of the lists is shorter or longer?
Still unclear: What is the meaning of the first digit in the list, most or least significant digit? This has pretty much influence on how your algorithm would look like!
For the rest of the question, I'll assume that first digit is least significant one, it is the case easier to handle:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2)
{
ListNode* head = nullptr; // we want to return head of newly created list but
ListNode* tail = nullptr; // need to append at its tail, so we need two pointers
while(l1 && l2) // yet elements existing in both lists?
{
// need to create new nodes!
ListNode* tmp = new ListNode(l1->val + l2->val);
if(!tail)
{
// this will be the first node we create!
head = tmp;
}
else
{
// we have a node already -> append
tail->next = tmp;
}
// in any case, new tail is the newly created node
tail = tmp;
l1 = l1->next;
l2 = l2->next;
}
// at this point, we worked through the common digits in both lists
// we yet need to create copies for those digits yet remaining in
// the longer of the two lists:
while(l1)
{
// create copy of *l1 and append
l1 = l1-> next;
}
while(l2)
{
// create copy of *l2 and append
l2 = l2-> next;
}
// of course, only one of the loops can be entered at all...
// be aware, though, that we might not have had elements in one of
// the lists right at start, so head and tail still might be nullptr,
// i. e. these two loops would look similar to the very first one!
return head;
}
What we have not yet considered at all is overflow. You would need to remember if it occurred and and add additionally + 1 in next loop run, if so.
来源:https://stackoverflow.com/questions/57354643/how-come-i-am-getting-this-error-while-trying-to-add-two-linked-lists