Segmentation Fault

I am trying to input a linked list using BuildList function but it is giving me segmentation fault. I am giving input as 1 2 3 4 5 -1.
What is wrong in my code?
My code - https://ide.codingblocks.com/s/241526

@lalchetna03
problem is associated with the insertAtTail() function. You have not added basecase i.e when head==NULL. So when your head is NULL, you code tries to check for it’s next which is not there and hence your code will give segmentation fault.
Just write these lines at the beginning of insertAtTail(), your code will work fine
if(head==NULL){
node *n =new node(data);
head = n;
return;
}

Okay, I got it now. Thank you!

1 Like