Reputation: 539
I have a node in a linked list defined as:
class Node {
public:
Node* next = nullptr;
int value;
};
I am inserting nodes into the list as:
void insertNode(Node* &head, int value) {
Node* newNode = new Node;
newNode->value = value;
newNode->next = head;
head = newNode;
}
I want to remove some node in that list in another function
void deleteNode(Node* head) {
// ...
}
How can I remove that now unused node from memory?
Upvotes: 1
Views: 47
Reputation: 31459
You'd remove a node by 1) delete
ing it. 2) adjusting the previous node to point to the one following the deleted node (or nullptr
if the deleted node was the last one).
Upvotes: 1