I understand the logic when the struct Node *head
is a global variable.
However, when I do the reverse using struct Node *head
as a local variable in main()
, I have to use double-pointer and pointer of Head
, and I don't understand where exactly I have to place them.
void Reverse(struct Node *Head, struct Node **headpointer) {
struct Node *first;
// when the list is empty
if (Head == NULL)
return;
first = Head;
// when there is one node left
if (first->next == NULL) {
*headpointer = first;
return;
}
Reverse(first->next, headpointer);
first->next->next = first;
first->next = NULL;
}
I am unclear why I have to use...
first = Head;
*headpointer = first;
why can't I just use
Head = first?
Also, if first = Head
line in front of the recursive function call Reverse(first->next, headpointer)
, don't the first->next
value also equal to Head
which points to the first
node?
Is there any good logical diagram/pic/explanation/examples that can explain this difference?