java实现链表的原地反转

解题思路

如果head.next为null 或者 head.next.next为null 就直接返回
1.头节点为空,我们设置temp=head.next.next;
2.让头节点的next指向空:head.next.next=null;(将第一个节点与后面的节点断开)
3.当temp不为空时,进入循环,首先定义一个节点cur=temp.next(为了后续的遍历)
4.让temp指向头节点的下一个节点(temp.next=head.next)
5.让头节点指向temp(head.next=temp)
6.让temp=cur,继续下一次循环

思路详解(字丑请多担待!)

在这里插入图片描述

代码实现

public void reverseBySelf(SingleLinkedList singleLinkedList){
        head=singleLinkedList.head;
        if (head.next==null||head.next.next==null){
            return;
        }
        HeroNode temp=head.next.next;
        head.next.next=null;
        while (temp!=null){
            HeroNode cur=temp.next;
            temp.next=head.next;
            head.next=temp;
            temp=cur;
        }
        
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值