typedef struct Node {
int data;
struct Node *next;
}List;
非递归:
List* ReverseList( List * head )
{
if(head == NULL || head->next == NULL)
{
return head;
}
List* p1 = head;
List* p2 = head->next;
p1->next =NULL;
while(p2)
{
List* p3 = p2;
p2 = p2->next;
p3->next = p1;
p1 = p3;
}
return p1;
}
递归:
List* rev( List * head )
{
List *rHead;
if( !head )
{
return head;
}
else if( !head->next ) //只有一个结点
{
return head;
}
else
{
rHead = rev( head->next );
head->next->next = head;
head->next = NULL;
return rHead;
}
}