Hello

I am not sure if my function below will completely delete a linked list. I am worried it will delete every element of the list except for the head(first node)?

For example; If I have this linked list below

L -> 1 -> 2 -> 3 -> 4 -> NULL

Will my function delete the 1st node (1)?

void destroy(node * & L)
{

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

Dani AI

Generated

Short answer: the first version does delete the head. In your original loop () you advance the temporary pointer to the next node before calling delete on the old head, so the old head is freed and L is updated to the saved next node. 's two-node walk-through shows that behavior correctly.

Clarifications and gotchas: 's suggestion to "swap the first two lines" would be unsafe — deleting a node and then trying to read its next is undefined behavior. 's revised routine that walks L->next and deletes each following node will work, but it must first check for an empty list (L == NULL) before doing L->next. Also ensure the function leaves the caller's head pointer null (the node *& L reference will be null when the loop finishes, but an explicit L = NULL (or nullptr) is fine as a clarity/safety step).

Troubleshooting tips:

  • Always save the next-pointer before deleting the current node; never dereference a pointer after delete.
  • If a node holds additional dynamic memory, make sure its destructor frees those members — delete only calls the node destructor.
  • Watch for double-delete and dangling pointers; after destruction the head should be set to NULL/nullptr.
  • Prefer encapsulating list ownership in a class destructor so deallocation is automatic and not spread around the code.

Modern alternative: use RAII/smart pointers so you do not manually delete nodes. For example, a singly-linked node with std::unique_ptr makes clearing automatic:

#include <memory>

struct Node {
int value;
std::unique_ptr<Node> next;
Node(int v) : value(v), next(nullptr) {}
};

// popping the front (old head destroyed automatically)
head = std::move(head->next);

See the standard documentation for details on delete semantics and std::unique_ptr: operator delete documentation and std::unique_ptr reference.

Recommended Answers

All 4 Replies

void destroy(node * & L)
{

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

It seems with the code you have posted you would indeed miss the first node in the list. If you swap the first two lines in the while loop it would delete all the nodes.

Also if you're wanting to delete L you should put L = temp in front of delete.

I have written a new code, I think this should delete all nodes right?

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

}

How come you don't just delete L? Is there a certain reason you're making a copy of the list? I think it would be easier just to delete L.

But yes, it looks like your code deletes all the nodes. :icon_cheesygrin:

Hello

I am not sure if my function below will completely delete a linked list. I am worried it will delete every element of the list except for the head(first node)?

For example; If I have this linked list below


Will my function delete the 1st node (1)?

void destroy(node * & L)
{

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

I think it will delete all nodes, lets see say node is 1->2->null , has 2 nodes

node *temp = L; //temp now is node1
    while(temp != NULL){ //while temp is != null
        temp = temp->next; //increment temp, temp is now node2
        delete L; //delete node1 
        L = temp; //head is now node2 
    }
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.