常见的三指针方法
struct node {
int data;
node * next;
};
node* inverseList(node *head){
if(head==NULL||head->next==NULL) return head;
node *pre = NULL;
node *cur = head;
node *next =NULL;
while(cur&&cur->next){
if(pre){
pre->next = cur->next;
}else{
head = cur->next;
}
pre = cur;
next = cur->next->next;
cur->next->next = cur;
cur->next = next;
cur = next;
}
return head;
}
node* createList(){
node* head=NULL;
node* cur=NULL;
node* pn;
for(int i=0;i<100;++i){
pn = new node;
pn->data = i;
if(cur==NULL)
{
head = pn;
cur = pn;
}else{
cur->next = pn;
cur = pn;
}
cur->next = NULL;
}
return head;
}
node* deleteList(node* head){
if(head==NULL) return NULL;
node * cur = head;
node * next = NULL;
while(cur){
next = cur->next;
delete cur;
cur = next;
}
return NULL;
}
void printList(node* head){
if(head){
printf("%d",head->data);
}
head = head->next;
while(head){
printf("->%d",head->data);
head = head->next;
}
printf("\r\n");
}
int _tmain(int argc, _TCHAR* argv[])
{
node *head=createList();
printList(head);
head = inverseList(head);
printList(head);
deleteList(head);
}