数据结构和算法——基于Java——3.2 链表练习题

1.获取到单链表的节点个数(若带头节点,需要不统计头节点)

在SingleLinkedList类中编写getLength方法

public static int  getLength(HeroNode head){
        if (head.next == null){//空链表
            return 0;
        }
        int length = 0;
        //定义辅助变流
        HeroNode temp = head.next;
        while (temp != null){
           length++;
           temp = temp.next;
        }
        return length;
    }

main方法调用

//linkedList.getHead()是SingleLinkedList内的getter方法返回头节点
System.out.println("有效节点个数:"+SingleLinkedList.getLength(linkedList.getHead()));

2.查找单链表中的倒数第k个节点

在SingleLinkedList类中编写findLastIndexNode方法

//查找单链表中的倒数第k个节点
    //思路:1.定义一个查找方法接收head节点,和index参数(就是倒数k参数)
    //2.先遍历一遍链表得到节点总的个数,再用length减去index就是倒数k个节点的个数
    //3.得到size后,我们从链表的第一个开始遍历(size-length)个
    //4.若找到返回一个节点,否则返回null
    public static HeroNode findLastIndexNode(HeroNode heroNode,int index){
        if (heroNode == null){
            return null;
        }
        //第一次遍历获取链表长度
        int length=0;
        HeroNode curr = heroNode.next;
        while (curr != null){
            length++;
            curr = curr.next;
        }
        //先做以下index的校验
        if (index<= 0 || index>length){
            return null;
        }
        curr = heroNode.next;//重置curr指针
        for (int i = 0; i < length - index; i++) {
            curr = curr.next;
        }
        return curr;
    }

main方法调用

System.out.println("倒数第3个为:"+SingleLinkedList.findLastIndexNode(linkedList.getHead(),3));;

3.单链表的反转

在SingleLinkedList类中编写reverseList方法

	//单链表的反转
    //思路:1.定义一个节点reverseHead = new HeroNode();
    //2.从头到位遍历原来的链表,每遍历一个节点,就将其取出放到新链表reverseHead的最前端
    //3.将新链表head.next = reverseHead.next
    public static void reverseList(HeroNode head){
        //没有节点或者只有一个节点直接返回
        if (head.next == null || head.next.next == null){
            System.out.println("链表为空!");
            return;
        }
        HeroNode cur = head.next;
        HeroNode next = null;
        HeroNode reverseHead = new HeroNode(0,"","");
        while (cur != null){
            next = cur.next;
            cur.next = reverseHead.next;
            reverseHead.next = cur;
            cur = next;
        }
        head.next = reverseHead.next;
    }

main方法中测试

		System.out.println("反转前:");
        linkedList.list();
        SingleLinkedList.reverseList(linkedList.getHead());
        System.out.println("反转后:");
        linkedList.list();

4.从尾到头打印单链表

思路:
方式1.先将单链表反转然后再遍历(破坏单链表结构,不推荐)
方式2.利用栈,将各个节点压入到栈中,利用栈的先进后出特点实现逆序打印

4.1 压栈方式实现

SingleLinkedList类中编写reversePrint方法

//逆序打印单链表
    //1.利用栈的先进后出思想实现,将单链表压栈再输出
    public static void reversePrint(HeroNode head){
        if (head.next == null){
            return;//空链表,退出程序
        }
        //创建一个栈,将单链表节点压栈
        Stack<HeroNode> stack = new Stack<>();
        //创建一个零时链表接收
        HeroNode cur = head.next;
        while (cur != null){
            stack.push(cur);//压栈
            cur = cur.next;//链表下移
        }
        while (stack.size()>0){
            System.out.println(stack.pop());//出栈
        }
    }

main方法测试

 		System.out.println("反转前:");
        linkedList.list();
        System.out.println("使用压栈反转后值为:");
        SingleLinkedList.reversePrint(linkedList.getHead());
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值