I am trying to remove duplicates from a linked list, and encountered a problem, which is probably obvious and straightforward but I haven\'t used C++
in many years
You cannot use free()
if you allocated memory with new
You can use malloc()
with free()
or new
with delete
. You also should not use malloc()
and free()
with objects as they do not call the objects constructor and destructor unlike new
and delete
So since you allocated the nodes with new
we can change
free(temp); //Here is my problem!!!
to
delete temp;