分类: C/C++
2011-04-16 08:29:06
单链表逆置算法
struct node
{
int num;
struct node *next;
}
struct node* reverse(struct node *head)
//head 链表头结点
{
struct node *p,*temp1,*temp2;
if(head==NULL____①____) return head; //||head->next==NULL
p=head->next;head->next=NULL;
while(____②____) //p!=NULL或p
{
temp1=head;
____③____; //head=p;
temp2=p;
p=p->next;
____④____; //temp2->next=temp1;或head->next=temp1;
}//Match while statenment
return head; //返回逆置后的链表的头结点
}
或者
struct node* reverse(struct node *head)
//head 链表头结点
{
struct node *p,*temp1,*temp2;
if(head==NULL____①____) return head; //||head->next==NULL
p=head->next;head->next=NULL;
while(____②____) //p!=NULL或p
{
temp1=p->next;
p->next = head;
head = p;
p = temp1;
}//Match while statenment
return head; //返回逆置后的链表的头结点
}