双向链表(3) - 反转双向链表

双向链表的反转过程,可以参考下面的例图。

(a) 原始双向链表


(b) 反转后的双向链表


下面是一个用于反转双向链表的简单方法。所需要做的事情就是交换每个节点的前向指针和后向指针,然后调整链表的头指针和尾指针。

#include <iostream>

struct Node {
    int data;
    Node* next; // 指向下一个节点
    Node* prev; // 指向前一个节点
};

//对链表进行反转
void reverse(Node** head) {
    Node* temp = NULL;
    Node* current = *head;

    //交换每个节点的后向指针和前向指针
    // 1-->2-->3, 假设2为current.
    while (current != NULL) {
        temp = current->prev; //temp=1
        current->prev = current->next; //3-->2
        current->next = temp;            //2-->1 
        current = current->prev; //3-->2-->1, current变为3,继续往后循环。
    }
    //总结:先处理前向指针,然后处理后向指针。这些操作都只对当前节点(current),不涉及其它节点。
    //1.缓存前向指针
    //2.将后向指针赋值给前向指针
    //3.将缓存的前者指针,赋值给后向指针
    //4.当前节点指针移动到下一个待处理节点

    //修改头指针之前,先检测链表是否为空链表,或者只有一个节点的情况
    if (temp != NULL)
        *head = temp->prev;
}

// 给定链表的头指针(head)以及一个整数,插入一个新的节点至链表的头部  
// 之所以传入双指针,因为函数中需要修改链表  
void push(Node** head, int newData) {
    //1. 分配新节点内存  
    Node* newNode = new Node;

    //2. 赋值  
    newNode->data = newData;

    //3. 将原始头节点做为新节点的后向指针,而前向指针置为NULL  
    newNode->next = (*head);
    newNode->prev = NULL;

    //4. 将原始头节点的前向指针置为新的节点  
    if ((*head) != NULL)
        (*head)->prev = newNode;

    //5. 将头指针置为新的节点  
    (*head) = newNode;
}

void printList(Node* head) {
    while (head != NULL) {
        std::cout << " " << head->data << " ";
        head = head->next;
    }
    std::cout << std::endl;
}


int main() {
    //初始化链表为:10<->8<->6<->4<->2<->0
    Node* head = NULL;
    push(&head, 0);
    push(&head, 2);
    push(&head, 4);
    push(&head, 6);
    push(&head, 8);
    push(&head, 10);

    std::cout << "Original DLL is: " << std::endl;
    printList(head);

    reverse(&head);

    std::cout << "Reversed DLL is: " << std::endl;
    printList(head);
    return 0;
}

运行结果:
Original DLL is:
 10  8  6  4  2  0
Reversed DLL is:
 0  2  4  6  8  10

时间复杂度:O(n)

  • 9
    点赞
  • 50
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是 Java 实现双向链表反转的代码示例: ```java class Node { int data; Node prev; Node next; Node(int data) { this.data = data; this.prev = null; this.next = null; } } class DoublyLinkedList { Node head; DoublyLinkedList() { this.head = null; } void insertAtEnd(int data) { Node newNode = new Node(data); if (head == null) { head = newNode; return; } Node temp = head; while (temp.next != null) { temp = temp.next; } temp.next = newNode; newNode.prev = temp; } void reverse() { if (head == null || head.next == null) { return; } Node current = head; Node temp = null; while (current != null) { temp = current.prev; current.prev = current.next; current.next = temp; current = current.prev; } if (temp != null) { head = temp.prev; } } void display() { Node temp = head; while (temp != null) { System.out.print(temp.data + " "); temp = temp.next; } System.out.println(); } } public class Main { public static void main(String[] args) { DoublyLinkedList dll = new DoublyLinkedList(); dll.insertAtEnd(10); dll.insertAtEnd(20); dll.insertAtEnd(30); dll.insertAtEnd(40); System.out.print("Original list: "); dll.display(); dll.reverse(); System.out.print("Reversed list: "); dll.display(); } } ``` 在上述代码中,我们首先定义了一个 `Node` 类来表示双向链表的节点,每个节点包含一个 `data` 值、一个指向前一个节点的 `prev` 指针和一个指向后一个节点的 `next` 指针。 接下来,我们定义了一个 `DoublyLinkedList` 类来表示双向链表。该类包含一个指向链表头部的 `head` 指针以及一些用于操作链表的方法。 其中,`insertAtEnd` 方法用于在链表末尾插入一个新的节点,`reverse` 方法用于反转链表,`display` 方法用于打印链表中所有节点的值。 在 `reverse` 方法中,我们使用了三个指针 `current`、`prev` 和 `temp`,其中 `current` 指向当前节点,`prev` 指向当前节点的前一个节点,`temp` 则用于保存下一个节点的位置。我们不断地遍历链表,将当前节点的 `prev` 和 `next` 指针交换,并将 `current` 指向下一个节点,直到遍历完整个链表。最后,如果链表非空,则将 `head` 指针指向反转后链表的新头部。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值