单链表反转

学习了单链表的一些简单操作,打卡插眼 

  //链表反转  接受头节点,然后对整个单链表进行反转,破坏链表结构
    public void reversetList(HeroNode head){

        if(head.next ==null || head.next.next == null)
            return;

        //定义辅助头节点:reversehead
        //定义辅助指针:cur, next
        HeroNode reversehead = new HeroNode(0,"","");
        HeroNode cur = head.next;
        HeroNode next = null;   //储存cur节点的下一个节点

        while(cur != null){

            next = cur.next;
            cur.next = reversehead.next;
            reversehead.next = cur;
            cur = next;

        }

        //将头节点指向已经反转的链表
        head.next = reversehead.next;

    }

 //实现对单链表的逆序打印,且不破坏链表结构
    public void resersePoint(HeroNode head){

        if(head.next == null)
            return;

        //cur: 辅助变量 stack:栈
        HeroNode cur = head.next;
        Stack<HeroNode> stack = new Stack<HeroNode>();

        //压入栈
        while(cur != null){

            stack.push(cur);
            cur = cur.next;

        }

        //出栈
        while(stack.size() > 0){

            System.out.println(stack.pop());

        }


    }

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值