Lee*_*ari -1 c++ struct initialization linked-list singly-linked-list
In this function, I get segmentation fault. I think it has something to do with memory allocation. What mistake am I making?
Now, if I initialize Node* a =NULL, i get my head pointer as NULL in the end.
struct Node {
int data;
struct Node* next;
Node(int x) {
data = x;
next = NULL;
}
};
Node* addTwoLists(Node* first, Node* second) {
// Code here
Node *a;
Node *head = a;
int bor = 0;
while(first->next && second->next) {
int ans = first->data + second->data;
a = new Node((ans%10)+bor);
bor = ans/10;
a=a->next;
first = first->next;
second = second->next;
}
return head;
}
Run Code Online (Sandbox Code Playgroud)
a is uninitialized. You must not use a until you assign a valuehead,因此它永远不会是别的。| 归档时间: |
|
| 查看次数: |
83 次 |
| 最近记录: |