6.单链表:递归反转链表

核心:Return在递归中是返回上一层

1.代码实现:

#include <stdlib.h>
#include <stdio.h>

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

struct Node *Insert(struct Node *head, int data)
{
    struct Node *Temp = (struct Node *)malloc(sizeof(struct Node));
    if (Temp == NULL)
    {
        printf("Memory allocation failed\n");
        return NULL; // 检查内存分配
    }
    Temp->data = data;
    Temp->next = NULL;

    if (head == NULL)
    {
        head = Temp; // 如果链表为空,直接将头指针指向新节点.修改了头指针,所以要返回
    }
    else
    {
        struct Node *current = head; // 使用临时指针遍历链表
        while (current->next != NULL)
        {
            current = current->next;
        }
        current->next = Temp; // 将新节点插入到链表末尾
    }
    return head;
}

struct Node *Delete(struct Node *head, int n)
{
    struct Node *Temp = head;
    if (Temp == NULL)
    {
        printf("Memory allocation failed\n");
        return NULL; // 检查内存分配
    }

    if (n == 1)
    {
        head = Temp->next;
        free(Temp);
        return head;
    }

    int i;
    for (i = 0; i < n - 2; i++) // 大于2,才往后移动
        Temp = Temp->next;

    struct Node *Temp2 = Temp->next;
    Temp->next = Temp2->next;
    free(Temp2);
    return head;
}

void Print(struct Node *head)
{
    // 逆序打印,满足条件后返回上一个的执行结果
    // if (head == NULL)
    //     return;                // 递归退出条件
    // Print(head->next);         // 递归循环开始,创建栈帧
    // printf("%d ", head->data); // 达到递归退出条件后,再从顶层栈帧往Print(head->next)语句后执行

    // 顺序打印,执行一次递归打印一次
    if (head == NULL)
        return;                // 递归退出条件
    printf("%d ", head->data); // 正常执行语句
    Print(head->next);         // 递归循环开始,创建栈帧。
    // 达到递归退出条件后,再从顶层栈帧往Print(head->next)语句后执行
}

struct Node *Reverse(struct Node *head)
{
    // 基本情况:如果链表为只有一个节点或链表走到最后节点
    if (head == NULL || head->next == NULL)
    {
        return head; // 返回当前节点作为新的头节点
    }

    struct Node *newHead = Reverse(head->next); // 递归反转后面的链表

    head->next->next = head; // 将下一个节点的 next 指向当前节点,将5作为6->next
    head->next = NULL;       // 将当前节点的 next 设置为 NULL,将NULL作为5->next

    return newHead; // 返回新的头节点
}

int main()
{
    struct Node *head = NULL;
    // 插入
    head = Insert(head, 1);
    head = Insert(head, 2);
    head = Insert(head, 3);
    head = Insert(head, 4);

    // 删除
    // head = Delete(head, 1);

    // 反转
    head = Reverse(head);

    // 打印
    Print(head);
    return 0;
}

2.难点:

struct Node *Reverse(struct Node *head)
{
    // 基本情况:如果链表为只有一个节点或链表走到最后节点
    if (head == NULL || head->next == NULL)
    {
        return head; // 返回当前节点作为新的头节点
    }

    struct Node *newHead = Reverse(head->next); // 递归反转后面的链表

    head->next->next = head; // 将下一个节点的 next 指向当前节点,将5作为6->next
    head->next = NULL;       // 将当前节点的 next 设置为 NULL,将NULL作为5->next

    return newHead; // 返回新的头节点
}

此时递归到达最末尾,也就是当前头节点的next为NULL,此时将当前位置标志位head节点

3.存在问题:

Question1:这里为什么要返回heap头节点

因为这里指针仍然是个临时拷贝值,所以改变头节点要告诉main函数,所以最后通过newHead返回告诉main函数

Question2:head->next->next是什么意思?

切记,这里易出错,第一次执行是返回return跳转到下一层栈帧

下一层栈帧指向的heap是5,所以这里的意思是:

6->next = 5;

5->next = NULL;

意思是交换两个值

Question3:你这个只是交换两个值的方向,但这个链表有多个值啊

对的,单纯交换两个数字是没用,因为这里面是多重指向,所以这里就继续进行交换,直至栈帧释放完成

4.原交换函数:(真TM绕)

struct Node *Reverse(struct Node *head)
{
    struct Node *current, *prev, *next; // 当前值、上一个、下一个

    current = head;         // 将头节点给当前值
    prev = NULL;            // 将上一个值赋值为NULL
    next = NULL;            // 将下一个值赋值为NULL
    while (current != NULL) // 如果当前值后面还有值
    {
        next = current->next; // 使用next保存后面的值
        current->next = prev; // 砍断与下一个值的联系,并把它连接在prev上
        prev = current;       // 更新反转链表的当前指向
        current = next;       // 将下一个值给用作当前值
    }
    head = prev; // 将头节点指向反转的链表
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值