单双链表反转

单链表反转

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

// 定义链表节点结构
struct Node {
    int data;
    struct Node* next;
};

// 反转链表函数
struct Node* reverseList(struct Node* head) {
    struct Node* prev = NULL;
    struct Node* current = head;
    struct Node* next = NULL;

    while (current != NULL) {
        next = current->next;  // 保存下一个节点的指针
        current->next = prev;  // 反转当前节点的指针
        prev = current;        // 更新 prev 指针
        current = next;        // 更新 current 指针
    }
    head = prev;  // 更新头指针为新的链表头

    return head;
}

// 输出链表内容函数
void printList(struct Node* node) {
    while (node != NULL) {
        printf("%d ", node->data);
        node = node->next;
    }
    printf("\n");
}

int main() {
    struct Node* head = NULL;
    struct Node* second = NULL;
    struct Node* third = NULL;

    // 分配节点内存
    head = (struct Node*)malloc(sizeof(struct Node));
    second = (struct Node*)malloc(sizeof(struct Node));
    third = (struct Node*)malloc(sizeof(struct Node));

    // 设置节点数据
    head->data = 1;
    head->next = second;

    second->data = 2;
    second->next = third;

    third->data = 3;
    third->next = NULL;

    printf("Original list: \n");
    printList(head);

    // 反转链表
    head = reverseList(head);

    printf("Reversed list: \n");
    printList(head);

    return 0;
}

双链表反转

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

// 定义双向链表节点结构
struct Node {
    int data;
    struct Node* next;
    struct Node* prev;
};

// 反转双向链表函数
struct Node* reverseDoublyLinkedList(struct Node* head) {
    struct Node* temp = NULL;
    struct Node* current = head;

    // 交换每个节点的 prev 和 next 指针
    while (current != NULL) {
        temp = current->prev;
        current->prev = current->next;
        current->next = temp;
        current = current->prev; // 移动到下一个节点
    }

    // 更新头指针为新的链表头
    if (temp != NULL) {
        head = temp->prev;
    }

    return head;
}

// 输出双向链表内容函数
void printDoublyList(struct Node* node) {
    struct Node* last = NULL;
    printf("\n从前往后打印链表:\n");
    while (node != NULL) {
        printf("%d ", node->data);
        last = node;
        node = node->next;
    }
    printf("\n从后往前打印链表:\n");
    while (last != NULL) {
        printf("%d ", last->data);
        last = last->prev;
    }
    printf("\n");
}

int main() {
    struct Node* head = NULL;
    struct Node* second = NULL;
    struct Node* third = NULL;

    // 分配节点内存
    head = (struct Node*)malloc(sizeof(struct Node));
    second = (struct Node*)malloc(sizeof(struct Node));
    third = (struct Node*)malloc(sizeof(struct Node));

    // 设置节点数据
    head->data = 1;
    head->prev = NULL;
    head->next = second;

    second->data = 2;
    second->prev = head;
    second->next = third;

    third->data = 3;
    third->prev = second;
    third->next = NULL;

    printf("Original list: ");
    printDoublyList(head);

    // 反转双向链表
    head = reverseDoublyLinkedList(head);

    printf("Reversed list: ");
    printDoublyList(head);

    return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

vegetablesssss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值