template<typename Type> void SingleList<Type>::reverse()
{
ListNode<Type> *p,*q,*r;
p=head;
q=p->m_pnext;
while(q!=NULL)
{
r=q->m_pnext;//用来保存q的后继结点
q->m_pnext=p;//实现逆置
p=q;
q=r;//p向后移动一个位子,指向q原来指向的结点
q=r; // 同时q也向后移动一个位子,指向r原来指向的结点
}
}
head->m_pnext->m_pnext=NULL;
head->m_pnext=p;
}
}
head->m_pnext->m_pnext=NULL;
head->m_pnext=p;
}
注意: head->m_pnext->m_pnext=NULL;
head->m_pnext=p;
不能为
head->m_pnext=Null;
head=p;这样会丢失原来你链表的尾节点,使得倒置后的新链表中尾节点错误。