思路
将原来的链表用头插法重新插入头节点后面
代码一
void inverseLink(SNODE *head)
{
SNODE *p,*q;
if(head->next!=NULL)//链表不空
{
p=head->next;
q=p;
head->next=NULL;
while(p!=NULL)//头插法重新插入
{
q=p->next;
p->next=head->next;
head->next=p;
p=q;
}
}
}//inverseLink
代码二
LNode *Inverse(LNode *L)
{
LNode *p, *q;
p = L->next;
L->next = NULL;
while (p != NULL)
{
q = p;
p = p->next;
q->next = L->next;
L->next = q;
}
return L;
}
代码三
/* Assuming no header and L is not empty. */
List ReverseList( List L )
{
Position c, n, p;
p = NULL;
c = L;
n = L->Next;
while( n != NULL )
{
c->Next = p;
p = c;
c = n;
n = n->Next;
}
c->Next = p;
return c;
}