我正在学习 CPP(第一语言),并且正在尝试反转链表。此代码不起作用
node* reverse(node** head){
node* previous=NULL;
node* current=*head;
node* nextptr=current->next;
while(current!=NULL){
current->next=previous;
previous=current;
current=nextptr;
nextptr=nextptr->next;
}
return previous;
}
这个正在工作
node* reverse(node** head){
node* previous=NULL;
node* current=*head;
node* nextptr;
while(current!=NULL){
nextptr=current->next;
current->next=previous;
previous=current;
current=nextptr;
}
return previous;
}
为什么第二个代码片段有效,而第一个无效?