leetcode206-反转链表

一.题目描述

给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。
输入:head = [1,2,3,4,5]
输出:[5,4,3,2,1]
输入:head = [1,2]
输出:[2,1]
输入:head = []
输出:[]

提示:

链表中节点的数目范围是 [0, 5000]
-5000 <= Node.val <= 5000

二.题目解析

1.递归法

public ListNode reverseList1(ListNode head) {
        /*递归方式,递推公式reverseList的含义是:把拿到的链表进行反转,然后返回新的头结点。
         * */
        //判断链表为空或者只有一个元素 || 递归终止条件
        if(head == null || head.next == null){
            return head;
        }
        //返回当前元素后面所有节点反转后的头结点(是固定的)
        ListNode leftHead = reverseList(head.next);
        //2 -->  3<--4   =>  2.next.next = 2 即实现了反转
        head.next.next = head;
        //head置为空返回到上一层递归调用会被重新赋值(head.next.next = head)
        head.next = null;
        //每层调用返回的leftHead是一样的
        return leftHead;

    }

在这里插入图片描述
2.双指针迭代遍历

 public ListNode reverseList(ListNode head) {
        /*迭代方式
        * */
        //判断链表为空或者只有一个元素
        if(head == null || head.next == null){
            return head;
        }
        ListNode pre = head;
        ListNode cur = head.next;
        ListNode nextPre = new ListNode();
        ListNode nextCur = new ListNode();
        while(cur != null){
            //由于后边cur指向会发生变化,先备份nextPre和nextCur
            nextCur = cur.next;
            cur.next = pre;
            if(pre == head){
                pre.next = null;
            }
            //更新指针
            pre = cur;
            cur = nextCur;
        }
        //最终的pre指向就是反转后的头结点
        return pre;

    }

在这里插入图片描述
这里还可以优化一下,不需要单独考虑头结点
令pre初始值为null,cur初始值是头结点即可

public ListNode reverseList(ListNode head) {
        /*迭代方式
        * */
        //判断链表为空或者只有一个元素
        if(head == null || head.next == null){
            return head;
        }
        ListNode pre = null;
        ListNode cur = head;
        ListNode nextPre = new ListNode();
        ListNode nextCur = new ListNode();
        while(cur != null){
            //由于后边cur指向会发生变化,先备份nextCur
            nextCur = cur.next;
            cur.next = pre;
            //更新指针
            pre = cur;
            cur = nextCur;
        }
        //最终的pre指向就是反转后的头结点
        return pre;

    }

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值