递归实现链表逆序(不需三个临时指针变量)

来源:互联网 发布:水电图设计软件 编辑:程序博客网 时间:2024/05/19 19:32
List *list_reverse(List *head) 
{
/*List *p,*q,*r; 
p=head; 
q=p->next; 
while(q!=NULL) 

r=q->next; 
q->next=p; 
p=q; 
q=r; 

head->next=NULL; 
head=p; 

return head; *///上为三个临时指针变量方法实现

List* new_head=head;
if(head==NULL || head->next==NULL)
        return head;
new_head = list_reverse(head->next);
head->next->next=head;
head->next=NULL;     //防止链表成为一个环,这是最关键的。
return new_head;
0 0
原创粉丝点击