please explain how------ node=(node->next);
deleting the node
Https://practice.geeksforgeeks.org/problems/delete-without-head-pointer/1
@Jun18APP0112 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.
@Jun18APP0112 hey,for array itβs quite different as you have to shift here is code for above explained logic :
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);
}
}
Dry run it and you will get it easily.