I have seen this in some books / textbooks.
When you pass a pointer to the head (linked list) in a function, you need to pass it as a double pointer.
For example: // This means to collapse the linked list, where head points to the first node.
void nReverse(digit **head)
{
digit *prev=NULL;
digit *curr=*head;
digit *next;
while(curr!=NULL)
{
next=curr->next;
curr->next=prev;
prev=curr;
curr=next;
}
*head=prev;
return;
}
This works great.
It also works when I use one pointer, for example
void nReverse(digit *head)
{
digit *prev=NULL;
digit *curr=head;
digit *next;
while(curr!=NULL)
{
next=curr->next;
curr->next=prev;
prev=curr;
curr=next;
}
head=prev;
return;
}
I tried to print the list with a pointer to the head. Both functions work fine.
Am I missing something?
Thank,
Johnh source
share