You are dereferencing tmp
instead of head
:
while (head)
{
auto tmp = head->next;
std::cout << tmp->data << std::endl;
delete head;
head = tmp;
}
should be:
while (head)
{
std::cout << head->data << std::endl;
auto tmp = head->next;
delete head;
head = tmp;
}
This is why 3
doesn't appear in your output and Segmentation fault
does.