- //链表逆序
- #include"stdlib.h"
- typedef struct code
- {
- int data;
- struct code *next;
- }Code;
- Code *Reverse(Code *head)
- {
- Code *cur,*pre,*temp;
- if(head==NULL||head->next==NULL)
- return head;
- cur=head;
- pre=NULL;
- while(cur)
- {
- temp=pre;
- pre=cur;
- cur=cur->next; //a a、b顺序不能反;
- pre->next=temp;//b
- }
- return pre;
- }
注意:a、b前后顺序不能倒置。若倒置,cur指向第一个元素,cur->next==NULL,循环中断。
阅读(314) | 评论(0) | 转发(0) |