这是一个使用递归方式反转链表,但是我最后调试后发现反转后的头指针位置没变化,请各位看看我哪里出问题了
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int data;
struct node *next;
}Node;
void Insert(Node **head,int x)
{
Node *temp=(Node*)malloc(sizeof(Node));
temp->data=x;
temp->next=NULL;
if(*head==NULL)
{
*head=temp;
return;
}else
{
Node *temp1=*head;
while(temp1->next!=NULL)
{
temp1=temp1->next;
}
temp1->next=temp;
}
}
void Print(Node *head)
{
while(head!=NULL)
{
printf("%d ",head->data);
head=head->next;
}
printf("\n");
}
Node *Reverse(Node *temp)
{
if(temp->next==NULL)
{
return temp;
}else
{
Reverse(temp->next);
Node *q=temp->next;
q->next=temp;
temp->next=NULL;
}
}
int main()
{
Node *head=NULL;
Insert(&head,1);
Insert(&head,2);
Insert(&head,3);
Insert(&head,4);
Insert(&head,5);
Print(head);
head=Reverse(head);
Print(head);
return 0;
}