There are two versions of singly linked list insertion function.
One is common version using one pointer and it's easy to understand:
void insert(struct node *newt) {
struct node *node = head, *prev = NULL;
while (node != NULL && node->data < newt->data) {
prev = node;
node = node->next;
}
newt->next = node;
if (prev == NULL)
head = newt;
else
prev->next = newt;
}
Another one is using pointer to pointer :
void insert(struct node *newt)
{
struct node **link = &head;
while (*link && (*link)->data < newt->data)
link = &(*link)->next;
newt->next = *link;
*link = newt; //confuse me
}
I am confused about *link = newt; Although I assign newt to *link, the previous node still point to the
original address?
Thank you in advance!