# 逆转单向链表

// Test.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
using namespace std ;

typedef struct Node{
int data;
struct Node *next;
}node;

{
{
return;
}

struct Node *pre,*cur,*nex;
while(cur)
{
nex = cur->next;
cur->next = pre;
pre = cur;
cur = nex;
}

}

{
if((!p)|| !(p->next))
{
return p;
}
else
{
temp->next = p;
p->next = NULL;
return p;
}
}

{
struct Node *first;
struct Node *rest;
{
return;
}
rest = first->next;
if(!rest)
{
return;
}
RecursiveReverse(&rest);
first->next->next = first;
first->next = NULL;
}

Node * G_p= NULL;
void RecursionReverse (Node * p1, Node *  p2 )
{
if ( p2 == NULL)
{
G_p= p1;
return ;
}

RecursionReverse( p2, p2->next);
p2->next= p1;

}

void RecursionReverse1 (Node  * &p1, Node *  p2 )
{
if ( p2 == NULL)
{
return ;
}
if ( p1 == p2)
{
p1= NULL;
}
Node *  p3= p1;
p1= p2;
RecursionReverse1( p1, p1->next);
p2->next= p3;

}
void RecursionReverse (Node * p1, Node *  p2 , Node *&  p)
{
if ( p2 == NULL)
{
p= p1;
return ;
}

RecursionReverse( p2, p2->next, p);
p2->next= p1;

}
{
{
}
}

void reverse2(Node * &top)
{
if (top)
{
Node* r = top , *p = r->next ,*s ;
r->next = NULL ;
if (!p)
{
cout <<"only 1 element!/n";
return   ;
}
else
{
if ( !p->next )
{
cout <<"2 elements!/n";
p->next = r;
return  ;
}
while (p)
{
s= p->next ;
if (!s)
{
p->next= r;
top= p;
return  ;
}
p->next= r ;
r= p;
p= s;
}
return  ;

}

}
else
{
cout <<"no element!/n";
return   ;
}
}

int _tmain(int argc, _TCHAR* argv[])
{
for ( int i= 1; i< 5; ++i)
{
node* pn= new node ;
pn->data= i;
p->next= pn;
p= pn;
}
p->next= NULL;

cout <<"逆转前:   ";

cout <<"/n逆转后:   ";

//方法二:void RecursionReverse (Node * p1, Node *  p2 ) //每二个进行换位:(^,0), 1, 2 ,3...

//方法三:void RecursionReverse1 (Node  * &p1, Node *  p2 )//第一个参数直接改变头指针head

//方法四:void RecursionReverse (Node * p1, Node *  p2 , Node *&  p)//第三个参数p只是最后一次调用,为了改变头指针head