链表节点定义:
struct ListNode{
int data;
struct ListNode *next;
};
链表反转的思路:从头到尾改变两个节点的指针方向,最后让最后一个节点调转指针方向。
ListNode* Reverse(ListNode *head){
ListNode* p=NULL;//记录被指向的节点位置
ListNode* cur=head;//记录要变换指针方向的节点
ListNode* next=cur->next;//记录下一个节点的位置
if(head==NULL) return NULL;
while(cur->next){
next =cur->next;
cur->next =p;
p = cur;
cur =temp;
}
cur->next=p;
return cur;
}