首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

求问上,这个链表倒置的代码是不是写错了

2012-09-10 
求问下,这个链表倒置的代码是不是写错了?C/C++ codevoid Reverse(node *&head){node *newheadNULLnode *

求问下,这个链表倒置的代码是不是写错了?

C/C++ code
void Reverse(node *&head){    node *newhead=NULL;    node *p;    while (head!=NULL)    {        p=head;        head=head->next;        p->next=newhead; //这句写错了吧?newhead咋在右边呢?        newhead=p;    }    head=newhead;}


先谢谢大神们了~~

[解决办法]
探讨
按照temp=a a=b b=temp来看

void Reverse(node *&head)
{
node *newhead=NULL;
node *p;
while (head!=NULL)
{
p=head;
head=head->next;
newhead=p;
p->next=newhead; //虽然也没错 但是倒过来比较……

热点排行