void deleteNode(Node *m)
{
// Your code here
m=(m->next);
}
how this code is working, this is the code to delete a particular node without giving the head node
void deleteNode(Node *m)
{
// Your code here
m=(m->next);
}
how this code is working, this is the code to delete a particular node without giving the head node
@firozbhaikardar21 hey,the trick here is we can copy the data of the next node to the data field of the current node to be deleted. Then we can move one step forward. Now our next has become the current node and current has become the previous node. Now we can easily delete the current node by conventional deletion methods.
For example, suppose we need to delete C and a pointer to C is given. If we had a pointer to B we could have deleted C easily. But here we will copy the data field of D to the data field of C. Then we will move forward. Now we are at D and we have a pointer to C i.e. the previous pointer. So we will delete D. That’s how the node C will be deleted.
The code which you have written is not complete:
void deleteNodeWithoutHead(struct Node* pos)
{
if (pos == NULL) // If linked list is empty
return;
else {
if (pos->next == NULL) {
printf("This is last node, require head, can't be freed\n");
return;
}
struct Node* temp = pos->next;
// Copy data of the next node to current node
pos->data = pos->next->data;
// Perform conventional deletion
pos->next = pos->next->next;
free(temp);
}
}
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.