两种方式实现链表反转(图解)

两种方式实现链表反转(图解)

循环法

思想:循环将相邻两节点反向。

直接上图
循环图解
看图所示,很明显循环的单位为

  • 标记游标 pre、current、next
  • 将当前节点指向前一节点(反转)
  • 移动游标
  • 重复以上步骤

解释:由于单链表无法从后一节点指向前一节点,故而需要保存前一节点(pre)用于反转的目标节点。

java代码实现

  /**
     * 链表反转
     */
    private static void reverseLinked(Node head) {
        Node pre = head;//第一步:标记游标
        Node current = head.next;//第一步:标记游标
        if (current == null) {
            return;
        }
        Node next = current.next;//第一步:标记游标
        current.next = pre;//第二步:节点反转
        pre.next = null;
        while (next != null) {
            pre = current;//第三步:游标后移
            current = next;
            next = current.next;
            current.next = pre;
        }
    }

看着有点重复代码,稍微改造一下吧:

 /**
     * 链表反转
     */
    private static void reverse(Node node) {
        Node pre = null;
        Node next = null;
        while (node != null) {
            next = node.next;
            node.next = pre;
            pre = node;
            node = next;
        }
    }

至此,反转结束,主要是通过标记循环节点,反转相邻节点,重复以上步骤。

递归法

递归图解
迭代的思想在于

假设 分步 细化

假设DGreverse的作用就是将给定的链表反转成你想要的样子,然后你需要就是给方法传参。
假设链表就两个节点,则你需要做的就是这样

 if (node == null || node.next == null) {
     return node;
  }
  Node temp = node;//保存头节点
  temp.next.next = node;//反转两节点
  node.next = null;//头节点置为尾节点

然后将假设的部分用递归代替:

Node newNode = DGreverse(node.next);

最终就是下面这样。

java代码实现

 /**
     * 递归方式链表反转
     *
     * @param node
     */
    private static Node DGreverse(Node node) {
        if (node == null || node.next == null) {
            return node;
        }
        Node temp = node;
        Node newNode = DGreverse(node.next);
        temp.next.next = node;
        node.next = null;
        return newNode;
    }

完活后带个值验证一下。不要去想递归是怎么实现的,就假设已经实现了,然后传参实现、验证,done!

### 链表反转算法的实现方法 链表反转是一个经典的算法问题,在不同的编程语言中有多种实现方式。以下是几种常见的方式及其具体实现。 #### 使用迭代的方法(以C为例) 在C语言中,可以使用三个指针变量`pre`、`cur`和`temp`来完成链表反转操作。初始时,`pre`设为`NULL`,表示新的链表头部;`cur`指向原始链表的头节点。每次循环中,先保存`cur->next`到临时变量`temp`,然后修改`cur->next`使其指向`pre`,最后更新`pre`和`cur`的位置以便处理下一个节点[^4]。 ```c struct ListNode* reverseList(struct ListNode* head) { struct ListNode* pre = NULL; struct ListNode* cur = head; while (cur != NULL) { struct ListNode* temp = cur->next; cur->next = pre; pre = cur; cur = temp; } return pre; } ``` #### 使用递归的方法(以Python为例) 递归方法的核心思想是从链表的尾部开始逐步构建反转后的链表。对于每个节点,将其后续部分视为已经反转好的子链表,然后调整当前节点与其后续节点之间的关系。 ```python def reverse_list(head): if not head or not head.next: return head reversed_head = reverse_list(head.next) head.next.next = head head.next = None return reversed_head ``` #### C#中的链表反转实现 在C#中,同样可以通过遍历链表并逐个更改节点间的链接方向来实现链表反转。这种方法类似于C语言中的迭代法[^3]。 ```csharp public class ListNode { public int val; public ListNode next; public ListNode(int x) { val = x; } } public static ListNode ReverseList(ListNode head) { ListNode prev = null; ListNode current = head; while(current != null){ ListNode nextTemp = current.next; current.next = prev; prev = current; current = nextTemp; } return prev; } ``` #### 双向链表反转 对于双向链表而言,除了需要交换前后两个方向上的指针外,还需要特别注意首尾节点的变化以及可能存在的边界条件处理[^5]。 ```java public void reverse() { Node<T> temp = null; Node<T> current = head; while (current != null) { temp = current.prev; current.prev = current.next; current.next = temp; current = current.prev; } if(temp != null ){ head = temp.prev; } } ``` 以上就是关于单向与双向链表反转的一些基本思路及其实现代码示例。每种语言都有自己的特点,因此具体的编码风格可能会有所差异,但核心逻辑保持一致。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值