I'm learning data structures in C++;
I've written a destructor for a linked list as follows:
~List(){
Node* temporary = new Node;
Node* node = head;
while(node != nullptr){
temporary = node;
node = node->next;
delete temporary;
}
}
But then I realized that I can do:
~List(){
Node* node = head;
while(node != nullptr){
node = std::move(node->next);
}
}
Avoiding creating a temporary object, I tried it and worked fine, but I don't know if it is okay, I didn't find a destructor like this in any other place.