正确写法:
Node* reverse(Node* head)
{
Node* current = head;
Node* reversList = null;
while(current)
{
/*将当前节点取出原链表迅速改变current(即current取出!!!注
意这是 一个赋值操作,赋值完后current改变了,p还是没变!!!)*/
Node *p = current;
current = current->next;
p->next = reversList;
reverseList = p;
}
return reverseList;
}
注意指针赋值!指针操作~~~~
错误写法:
Node *p = current;
//赋值操作,p->next 和current->next是同一
//个值。这一步就改变了current->next,出错!!!
p->next = reversList;
reverseList = p;
current = current->next;
这样p->next = reverlist已经把后面的改了,current找不到原来的链了。
current = current->next;事实上,当原来的链不再用到时,要迅速改变current的值!!!!