递归法反转部分链表,我先要只反转前2个,打印结果只打印出反转的结果,后续为反转的没有打印出来,请大家指证我的错误。
```c
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int data;
struct node *next;
}Node;
//Insert 创建节点插入数据
//Print 打印数据
//ReverseN 打印前N个数据
void Insert(Node **head,int x);
void Print(Node *head);
Node *ReverseN(Node *head,int n);
int main()
{
Node *head=NULL;
Insert(&head,1);
Insert(&head,2);
Insert(&head,3);
Insert(&head,4);
Print(head);
int n;
//printf("Which position you want to reverse?");
//scanf("%d",&n);
head=ReverseN(head,2);
Print(head);
return 0;
}
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 *ReverseN(Node *head,int n)
{
Node *temp;
if(n==1)
{
temp=head->next;
return head;
}
Node *fuck=ReverseN(head->next,n-1);
head->next->next=head;
head->next=temp;
return fuck;
}
```