Hello

I have 2 ways to delete a linked list, which one is correct?

If I have this linked list:


list = 1 - 2 - 3- 4

Which function will delete list correctly?
1.

void destroy(node * & list)
{
 
     while (list != NULL) {
            delete list;
            list = list->next;
     }

}

2.

void destroy(node * & list)
{ 
     delete list;
}

Dani AI

Generated

Both of the snippets posted by are unsafe. The version that calls delete before reading the node's next pointer risks accessing freed memory (undefined behavior). The single delete call only frees the head and leaks the remaining nodes. pointed to the right approach: walk the list and destroy every node. The variant that advances a temporary pointer to the next node before deleting the current node will still delete the final element — the important rule is to obtain the next pointer before destroying the current node.

A safe, manual deletion strategy in words: iterate while the current node is not null, save the current node's next pointer first, destroy the current node, then advance to the saved next pointer. If the function must null out the caller's head, accept the head by reference (or pointer-to-pointer) and set it to nullptr at the end. Deleting a null pointer is safe, but accessing a pointer after it has been deleted is undefined behavior. Also watch for double-delete and shared ownership — each node should have exactly one owner during manual management.

For modern, safer code prefer RAII: express ownership with std::unique_ptr for next pointers or use the standard containers (std::forward_list/std::list) so destruction is automatic. See delete — cppreference and std::unique_ptr — cppreference for details.

Recommended Answers

All 2 Replies

Neither.

Its should look something like this :

~LinkedList(){
    List * temp = head;
     while(temp != null){
               temp = temp->next;
               delete head;
               head = temp;              
     }
}

Thanks for ur reply :)

One problem tho. Wont the code above, not delete the very last element in the linked list? Because of the 1st line within the loop -
temp = temp->next; will cause the while loop to break/stop before we get to delete head??

void destroy(node * & L)
{
     
     node *temp = L;
     while(temp != NULL){
               temp = temp->next;
               delete L;
               L = temp;              
     }

     // delete L;   //do we need to put this 
                     //in to delete the very last element??
}

Also is this(my) method better or worse?

void destroy(node * & L)
{
     
     node *del = L->next;
     
     while (del != NULL) {
           L->next = del->next;
           delete del;
           del = L->next;
     }
     
     delete L;  // Delete the head of the Linked List
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.