单向链表的反转(reverse)

现在有一个单向链表,我们要想办法将这个当前链表的节点反转过来

在这里插入图片描述
思路分析:

  1. 先定义一个空节点reserveHead
  2. 定义一个辅助节点cur=head.next,以及一个空节点next记录cur节点的下一个节点(循环需要)
  3. 遍历链表,每遍历到一个cur节点,将这个节点指向reserveHead节点的下一个节点(第一次不需要指向,因为第一次reserveHead节点为空)。
  4. 将reserveHead指向cur节点
  5. 将head节点指向reserveHead节点的下一个节点,完成链表反转

代码实现:
1.定义一个节点类

class HeroNode {
    public int no;
    public String name;
    public String nikename;
    public HeroNode next;//指向下一个节点

    //构造器
    public HeroNode(int no, String name, String nikename) {
        this.no = no;
        this.name = name;
        this.nikename = nikename;
    }
    //为了显示方便,我们重写一下tostring方法


    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nikename='" + nikename + '\'' +

                '}';
    }
}

2.定义一个SingleLinkedList(链表类)

class SingleLinkedList {
    //先初始化一个头结点,头结点不能动
    private HeroNode head = new HeroNode(0, "", "");
    }

3.定义显示链表的方法

 public void List() {
        //判断链表是否为空

        if (head.next == null) {
            System.out.println("链表为空");
           // return;
        }
        HeroNode temp = head.next;
        while (true) {
            //判断是否到链表最后
            if (temp == null) {
                break;
            }

            System.out.println(temp);
            //将temp后移
            temp = temp.next;
        }

4.定义链表反转的方法

public void reverse(SingleLinkedList singleLinkedList){
        head=singleLinkedList.head;  
        if (head.next==null||head.next.next==null){
            return;
        }
        HeroNode cur=head.next;
        HeroNode next=null;
        HeroNode reserveHead=new HeroNode(0,"","");
        while (cur!=null){
            next=cur.next;
            cur.next=reserveHead.next;
            reserveHead.next=cur;
            cur=next;
        }
        head.next=reserveHead.next;
    }

5.测试

 public static void main(String[] args) {
        HeroNode heroNode1=new HeroNode(1,"宋江","及时雨");
        HeroNode heroNode2=new HeroNode(2,"卢俊义","玉麒麟");
        HeroNode heroNode3=new HeroNode(3,"吴用","智多星");
        HeroNode heroNode4=new HeroNode(4,"林冲","豹子头");

        SingleLinkedList singleLinkedList = new SingleLinkedList();
        singleLinkedList.addByOrder(heroNode1);
        singleLinkedList.addByOrder(heroNode2);
        singleLinkedList.addByOrder(heroNode3);
        singleLinkedList.addByOrder(heroNode4);
       //测试修改功能
        //singleLinkedList.update(new HeroNode(5, "小宋", "及时雨"));
        //测试删除功能
       // singleLinkedList.del(8);
        //测试链表的反转
        System.out.println("反转之前的链表");
        singleLinkedList.List();
        System.out.println("反转过后的链表");
        singleLinkedList.reverse(singleLinkedList);
        singleLinkedList.List();
    }
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值