这个链表倒置的算法请大家分析下
void Inverse(Node *&head)
{
if(!head) return;
Node *Next = head->next;
Node *Prev = NULL;
head->next = Prev;
while(Next){
Prev = head;
head = Next;
Next = Next->next;
head->next = Prev;
}
}
对指针十分不理解。本来我觉得是理解的,但是这个代码里完全没有头绪了。理不清。还请详细讲解。
答案:void Inverse(Node *&head)
{
if(!head) return;
Node *Next = head->next; next指向头指针后的第一个有效接点.如node1
Node *Prev = NULL;
head->next = Prev; 头不在指向第一节点.而用null使其成为连表结尾标志
while(Next){
Prev = head; pre变为头指针
head = Next; 原来的头指针指向head->next.如node1
Next = Next->next; next向后移一个接点.如node2
head->next = Prev;头指针head再次变为真正的头
上一个:为什么这个程序求不出公式?
下一个:c语言函数库调用