首页 > 其他分享 >单链表面试题

单链表面试题

时间:2022-11-28 13:56:47浏览次数:46  
标签:head 单链 return 试题 temp HeroNode next 表面 null

单链表的面试题

image-20221127214900597

  • 1.求单链表中的有效节点的个数
 /**
     *
     * @param heroNode  链表的头结点
     * @return 返回的就是有效节点的个数
     */
    public static int getLength(HeroNode heroNode){
        if (heroNode.next == null){
            return 0;
        }
        int length = 0;
        HeroNode temp = heroNode.next;
        while (temp != null){
            length++;
            temp = temp.next; //遍历整个链表
        }
        
        return length;
    }
  • 2.查找单链表中的倒数第k个节点
 /**
     * 查找单链表中的倒数第k个节点
     * @param index
     * @param heroNode
     * @return
     */
    public  static HeroNode getNodeByInedx(int index,HeroNode heroNode){
        if (heroNode.next == null){
            return  null;
        }
        int size = getLength(heroNode);
        //第二次遍历
        if(index < 0 || index >= size){
            return  null;
        }
        HeroNode temp = heroNode.next;
        for (int i = 0; i < size-index; i++) {
            temp = temp.next;
        }

        return temp;
    }
  • 3.单链表的反转

image-20221128104544566

   /**
     * 链表反转
     * @param head
     */
    public static  void reverseSingleLinkedList(HeroNode head){
        if (head == null || head.next.next == null) {
            return;
        }
        HeroNode cur = head.next;
        HeroNode next = null;
        HeroNode reversHead = new HeroNode(0,"","");
        while (cur.next !=null){
            next = cur.next;
            cur.next = reversHead.next;
            cur = next;//后移

        }
        
        head.next  = reversHead.next ;
    }
  • 从头到尾打印链表

image-20221128104218521

 /**
     * 逆序打印链表
     * @param head
     */
    public static void reversPrint(HeroNode head){
        if (head == null ) {
            return;
        }
        Stack<HeroNode> heroNodes = new Stack<>();
        HeroNode temp = head.next;
        while (temp != null){
            heroNodes.push(temp);
            temp = temp.next;
        }
       while (heroNodes.size()>0){
           System.out.println(heroNodes.pop());
       }
    }

标签:head,单链,return,试题,temp,HeroNode,next,表面,null
From: https://www.cnblogs.com/wiseleer/p/16931986.html

相关文章

  • 格力2020秋招网络安全岗笔试题
    1.访问控制能够有效地防止对资源的非授权访问,一个典型的访问控制规则不包括()A主体B客体C认证D控制策略 访问控制包括三个要素:主体、客体和控制策略。......
  • 肖sir___阿里面试题__笔试题
    身份证:330184198903193110籍贯:浙江省杭州市余杭区良渚街道之前薪资12K期望薪资13K最近公司地址:杭州市西湖区文一西路522号(杭州百图科技有限公司)之前公司地址:杭州市拱墅区赵......
  • 作用域和闭包常见的面试题
    作用域和闭包常见的面试题作用域变量提升varscope="global";functionscopeTest(){console.log(scope);varscope="local"}scopeTest();//undefined......
  • 面试题收集
    秋招过后,根据学习群里很多小伙伴的反馈,发现在Java后端面试中,Redis是所有框架/中间件中被问到频率最高的。但是由于本身不熟悉Redis,再加上大厂的面试题也确实是很难,有些还......
  • SpringBoot面试题
    1SpringBoot启动Tomcat1.1Spring在启动时创建一个Spring容器1.2利用@ConditionalOnClass技术判断classpath中是否存丰Tomcat依赖,如果存在则生成一个启动Tomcat的Bean1.......
  • 面试题系列:MQ 夺命连环11问
      1.你们为什么使用mq?具体的使用场景是什么? mq的作用很简单,削峰填谷。以电商交易下单的场景来说,正向交易的过程可能涉及到创建订单、扣减库存、扣减活动预算、扣......
  • Linux面试题3:Linux零拷贝技术
    zero-copy技术Linux网络IO数据传输过程图整个操作过程中,做了四次用户态和内核态的状态切换,数据从网卡copy到内核缓冲区,再从内核缓冲区copy到user-space;写入时从user-spa......
  • 宝宝精刷题笔记 面试题 02.05. 链表求和
    题目描述给定两个用链表表示的整数,每个节点包含一个数位。这些数位是反向存放的,也就是个位排在链表首部。编写函数对这两个整数求和,并用链表形式返回结果。示例1:输入......
  • 【面试题】 面试官为啥总是喜欢问前端路由实现方式?
    背景从事前端开发的同学,在实际工作中,一定会接触过路由这个概念,同时在面试的过程中,经常会被问及关于前端路由的实现方式,面试官到底想考察什么?在现在SPA单页面模式盛行,前后端......
  • 准备面试题【面试】
    前言写作于2022-11-1319:27:08发布于2022-11-2016:34:44准备程序员囧辉​​我要进大厂​​​​面试阿里,HashMap这一篇就够了​​​​Java基础高频面试题(2022年最新版)......