Ptr Reverse(Ptr head,int K)
{ cnt=1;
new=head->next;
old=new->next;
while(cnt<K)
{ tmp=old->next;
old->next=new;
new=old;
old=temp;
cnt++;
}
head->next->next=old;
return new;
}下面是有关程序代码:
Node * ReverseList(Node *head)
{
Node *new,*old,*tmp;
if(head==NULL||*head==NULL)
return head;
new=head;
old=new->next;
while(old) //注意条件
{
tmp=old->next; //要改变old->next的指针,所以必须先保留old->next
old->next=new;
new=old; //循环往后
old=tmp;
}
head->next=NULL; //原先的head已经变成tail,别忘了置空,只有到这步才能置空
*head=new;
return head;
}
本文介绍了一种链表逆序的实现方法,通过迭代方式重新调整链表节点间的连接顺序,实现了链表的有效反转。
1852

被折叠的 条评论
为什么被折叠?



