Why am I getting segmentation fault while implementing linked list?

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)

cra*_*str 6

  1. a is uninitialized. You must not use a until you assign a value
  2. 您再也不会分配给head,因此它永远不会是别的。