反转链表(采用递归或迭代实现)

  •  出处

206. 反转链表

给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。

示例 1:

输入:head = [1,2,3,4,5]
输出:[5,4,3,2,1]

示例 2:

输入:head = [1,2]
输出:[2,1]

示例 3:

输入:head = []
输出:[]
  • 思路:

本题 核心:迭代法 或 递归

  • ListNode 类

public class ListNode {
    int val;
    ListNode next;

    ListNode() {
    }

    ListNode(int val) {
        this.val = val;
    }

    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}
  • 迭代法 

/**
 * @author:Herman
 * @since 2025/9/27 0:25
 */
public class ReverseLinkedList {

    public static ListNode reverseList(ListNode head) {
        //前驱节点
        ListNode pre = null;

        //当前节点
        ListNode cur = head;

        while (cur != null) {
            // 后继节点
            ListNode next = cur.next;
            // 反转当前节点指针
            cur.next = pre;
            // 移动前驱节点
            pre = cur;
            // 移动当前节点
            cur = next;

            /**
             *                 head=0x001 (val=1)    pre =null           cur=0x001 (val=1)
             *
             * 1->null         next=0x002  (val=2)   pre=0x001 (val=1)   cur=0x002 (val=2)
             *
             * 2->1->null      next=0x003  (val=3)   pre=0x002 (val=2)   cur=0x003 (val=3)
             *
             * 3->2->1->null   next=null             pre=0x003 (val=3)   cur=null
             */

        }
        return pre;
    }


    public static void printListNode(ListNode head) {
        StringBuilder sb = new StringBuilder("[");
        while (head != null) {
            sb.append(head.val);
            head = head.next;
            if (head != null) {
                sb.append(",");
            }
        }
        sb.append("]");
        System.out.println(sb);
    }
}


  • 递归法 

/**
 * @author:Herman
 * @since 2025/9/27 0:55
 */

public class ReverseLinkedList {


    public static ListNode reverseList(ListNode head) {
        // 原链表为:1 -> 2 -> 3 -> 4 -> 5

        // 递归终止条件 ( 当链表为空或只有一个节点时,直接返回该节点 )
        if (head == null || head.next == null) {
            return head;
        }

        // 递归调用 ( 将2 -> 3 -> 4 -> 5这部分交给递归处理,最终返回的newHead是5 )
        ListNode newHead = reverseList(head.next);


        // 调整指针关系

        // 原来2的next指向1
        head.next.next = head;
        // 1的next置为null
        head.next = null;  
        
        /*
        原始链表: 1 -> 2 -> 3 -> 4 -> 5

        递归到最深处:
        - reverseList(5): 返回5 (因为5.next == null)
        
        回溯过程:
        - 处理4: 4 -> 5 变成 5 -> 4
        - 处理3: 3 -> 4 变成 5 -> 4 -> 3
        - 处理2: 2 -> 3 变成 5 -> 4 -> 3 -> 2
        - 处理1: 1 -> 2 变成 5 -> 4 -> 3 -> 2 -> 1
        */

        return newHead;
    }


    public static void printListNode(ListNode head) {
        StringBuilder sb = new StringBuilder("[");
        while (head != null) {
            sb.append(head.val);
            head = head.next;
            if (head != null) {
                sb.append(",");
            }
        }
        sb.append("]");
        System.out.println(sb);
    }
}


  • 测试代码


public class ReverseLinkedListTest {
    public static void main(String[] args) {

        ListNode head =
                new ListNode(1,
                        new ListNode(2,
                                new ListNode(3,
                                        new ListNode(4,
                                                new ListNode(5)))));
        //打印原始链表
        ReverseLinkedList.printListNode(head);
        //反转链表
        ListNode handledHead = ReverseLinkedList.reverseList(head);
        //打印反转后的链表
        ReverseLinkedList.printListNode(handledHead);

    }
}

测试结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值