messiuuuu 2023-12-25 20:42 采纳率: 95.2%
浏览 4

递归法反转部分链表问题

递归法反转部分链表,我先要只反转前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;
}

```

  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2023-12-25 22:20
    关注

    【相关推荐】




    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    评论

报告相同问题?

问题事件

  • 创建了问题 12月25日

悬赏问题

  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件