1.链表的数据结构
typedef struct node{
int data;
struct node* next;
} node;
2.实现思路
如下,有3个指针,pre,cur,last
i.开始先将第一个指针指向NULL,
ii.让cur指向pre,然后循环后移3个指针,让cur指向pre,直到last==NULL就结束
iii.最后的情况,当last==NULL时
此时,让cur指向pre,大功告成。
代码如下:
node * reverse(node *head){
if(head == NULL)
return NULL;
else if(head->next == NULL)
return head;
node *pre,*cur,*last;
pre = head;
cur = pre->next;
last = cur->next;
pre->next = NULL;
while(last != NULL){
cur->next = pre;
pre = cur;
cur = last;
last = last->next;
}
cur->next = pre;
head=cur;
return cur;
}