【数据结构】关于单链表的面试题

关于单链表的面试题

1、求单链表中有效节点的个数
2、查找单链表中的倒数第k个节点
3、单链表的反转
4、逆序打印链表信息

1、求单链表中有效节点的个数

1.1 代码编写

public class SingleLinedList_NodeNumber {
    public static void main(String[] args) {
        HeroNode hero1 = new HeroNode(1, "宋江", "及时雨");
        HeroNode hero2 = new HeroNode(2,"卢俊义","玉麒麟");
        HeroNode hero3 = new HeroNode(4,"吴用","智多星");
        HeroNode hero4 = new HeroNode(3,"林冲","豹子头");
        SingleLikedList singleLikedList = new SingleLikedList();
        singleLikedList.addByOrder(hero1);
        singleLikedList.addByOrder(hero2);
        singleLikedList.addByOrder(hero3);
        singleLikedList.addByOrder(hero4);
        singleLikedList.show();
        System.out.println("有效节点个数:"+singleLikedList.getLength(singleLikedList.getHead()));
    }
}
//定义SingleLinkedList来管理英雄
class SingleLikedList {
    //先初始化一个头节点,头节点不要动,不存放具体数据
    private HeroNode head = new HeroNode(0, "", "");
    //返回头节点
    public HeroNode getHead(){
        return head;
    }
    public void addByOrder(HeroNode heroNode) {
        //因为head节点不能动,因此需要新建一个辅助变量temp,temp是位于添加位置的前一个节点
        HeroNode temp = head;
        boolean flag = false;//flag标识添加的编号是否存在,默认为false
        while (true) {
            if (temp.next == null) {//说明temp在链表最后
                break;
            }
            if (temp.next.no > heroNode.no) {//位置找到,就在temp后面插入
                break;
            } else if (temp.next.no == heroNode.no) {//说明希望添加的heroNode的编号已然存在
                flag = true;//说明编号存在
                break;
            }
            temp = temp.next;//temp后移,遍历当前链表
        }
        //判断flag的值
        if (flag) {
            System.out.printf("准备插入节点的编号%d已经存在了,不能再插入\n", heroNode.no);
        } else//可以插入到链表中,在temp的后面
        {
            heroNode.next = temp.next;
            temp.next = heroNode;
        }
        //当退出while循环时,temp就指向了链表最后节点,将temp的next指向新的节点
        temp.next = heroNode;
    }

    //显示链表(遍历)
    public void show() {
        //判断链表是否为空
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        //因为head节点不能动,因此需要新建一个辅助变量temp
        HeroNode temp = head.next;
        while (true) {
            //判断是否到链表最后
            if (temp == null) {
                break;
            }
            //输出节点信息
            System.out.println(temp);
            //将temp后移
            temp = temp.next;
        }
    }
    //获取到单链表有效节点的个数(如果是带头节点的链表,需求不统计头节点)
    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;
    }

}

//定义HeroNode类,每个HeroNode对象就是一个节点
class HeroNode{
    public int no;
    public String name;
    public String nickname;
    public HeroNode next;//指向下一个节点

    //构造器
    public HeroNode(int no,String name,String nickname){
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }
    //重写toString方法
    public String toString() {
        return "HeroNode[no=" + no + ",name=" + name + ",nickname=" + nickname + "]";
    }
}

1.2 运行结果

在这里插入图片描述

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

2.1 解题思路

(1)编写一个方法,接收head节点,同时接收一个index;
(2)index表示是倒数第index个节点;
(3)先把链表从头到尾遍历,得到链表的总长度getLength;
(4)得到size后,从链表的第一个节点开始遍历(size-index)个。

2.1 代码编写

public class SingleLinedList_FindKNode {
    public static void main(String[] args) {
        HeroNode hero1 = new HeroNode(1, "宋江", "及时雨");
        HeroNode hero2 = new HeroNode(2,"卢俊义","玉麒麟");
        HeroNode hero3 = new HeroNode(4,"吴用","智多星");
        HeroNode hero4 = new HeroNode(3,"林冲","豹子头");
        SingleLikedList singleLikedList = new SingleLikedList();
        singleLikedList.addByOrder(hero1);
        singleLikedList.addByOrder(hero2);
        singleLikedList.addByOrder(hero3);
        singleLikedList.addByOrder(hero4);
        singleLikedList.show();
        System.out.println("有效节点个数:"+singleLikedList.getLength(singleLikedList.getHead()));
        System.out.println(singleLikedList.getKNode(singleLikedList.getHead(),2));
    }
}
//定义SingleLinkedList来管理英雄
class SingleLikedList {
    //先初始化一个头节点,头节点不要动,不存放具体数据
    private HeroNode head = new HeroNode(0, "", "");

    //返回头节点
    public HeroNode getHead() {
        return head;
    }

    public void addByOrder(HeroNode heroNode) {
        //因为head节点不能动,因此需要新建一个辅助变量temp,temp是位于添加位置的前一个节点
        HeroNode temp = head;
        boolean flag = false;//flag标识添加的编号是否存在,默认为false
        while (true) {
            if (temp.next == null) {//说明temp在链表最后
                break;
            }
            if (temp.next.no > heroNode.no) {//位置找到,就在temp后面插入
                break;
            } else if (temp.next.no == heroNode.no) {//说明希望添加的heroNode的编号已然存在
                flag = true;//说明编号存在
                break;
            }
            temp = temp.next;//temp后移,遍历当前链表
        }
        //判断flag的值
        if (flag) {
            System.out.printf("准备插入节点的编号%d已经存在了,不能再插入\n", heroNode.no);
        } else//可以插入到链表中,在temp的后面
        {
            heroNode.next = temp.next;
            temp.next = heroNode;
        }
        //当退出while循环时,temp就指向了链表最后节点,将temp的next指向新的节点
        temp.next = heroNode;
    }

    //显示链表(遍历)
    public void show() {
        //判断链表是否为空
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        //因为head节点不能动,因此需要新建一个辅助变量temp
        HeroNode temp = head.next;
        while (true) {
            //判断是否到链表最后
            if (temp == null) {
                break;
            }
            //输出节点信息
            System.out.println(temp);
            //将temp后移
            temp = temp.next;
        }
    }

    //获取到单链表有效节点的个数(如果是带头节点的链表,需求不统计头节点)
    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;
    }

    //查找单链表中的倒数第K个节点
    public HeroNode getKNode(HeroNode head, int index) {
        if (head.next == null) {   //空链表
            return null;
        }
        //先做一个index数据检验
        int size = SingleLikedList.getLength(head);
        if (index <= 0 || index > size) {
            System.out.println("输入K值有误");
            return null;
        }
        HeroNode temp = head.next;
        int n = 0;
        //第二次遍历size-index位置
        while (true) {
            if (n == size - index) {
                break;
            }
            temp = temp.next;
            n++;
        }
        return temp;
    }
}
    //定义HeroNode类,每个HeroNode对象就是一个节点
    class HeroNode {
        public int no;
        public String name;
        public String nickname;
        public HeroNode next;//指向下一个节点

        //构造器
        public HeroNode(int no, String name, String nickname) {
            this.no = no;
            this.name = name;
            this.nickname = nickname;
        }

        //重写toString方法
        public String toString() {
            return "HeroNode[no=" + no + ",name=" + name + ",nickname=" + nickname + "]";
        }
    }



2.2 运行结果

在这里插入图片描述

3、单链表的反转

3.1 解题思路

思路:
(1)先定义一个节点reverseHead =new HeroNode();
(2)从头到尾遍历原来的链表,每遍历一个节点,就将其取出,并放在新的链表reverseHead的最前端;
(3)原来的链表的head.next =reverseHead.next
第一次移动和第二移动示意如下图所示:
在这里插入图片描述
最终效果如下图所示
最终效果

3.2 代码编写

public class SingleLinedList_Reverse {
    public static void main(String[] args) {
        HeroNode hero1 = new HeroNode(1, "宋江", "及时雨");
        HeroNode hero2 = new HeroNode(2,"卢俊义","玉麒麟");
        HeroNode hero3 = new HeroNode(4,"吴用","智多星");
        HeroNode hero4 = new HeroNode(3,"林冲","豹子头");
        SingleLikedList singleLikedList = new SingleLikedList();
        singleLikedList.addByOrder(hero1);
        singleLikedList.addByOrder(hero2);
        singleLikedList.addByOrder(hero3);
        singleLikedList.addByOrder(hero4);
        System.out.println("====反转前遍历链表信息====");
        singleLikedList.show();
        System.out.println("有效节点个数:"+singleLikedList.getLength(singleLikedList.getHead()));
        singleLikedList.Reverse(singleLikedList.getHead());
        System.out.println("====反转后遍历链表信息====");
        singleLikedList.show();
    }
}
//定义SingleLinkedList来管理英雄
class SingleLikedList {
    //先初始化一个头节点,头节点不要动,不存放具体数据
    private HeroNode head = new HeroNode(0, "", "");

    //返回头节点
    public HeroNode getHead() {
        return head;
    }

    public void addByOrder(HeroNode heroNode) {
        //因为head节点不能动,因此需要新建一个辅助变量temp,temp是位于添加位置的前一个节点
        HeroNode temp = head;
        boolean flag = false;//flag标识添加的编号是否存在,默认为false
        while (true) {
            if (temp.next == null) {//说明temp在链表最后
                break;
            }
            if (temp.next.no > heroNode.no) {//位置找到,就在temp后面插入
                break;
            } else if (temp.next.no == heroNode.no) {//说明希望添加的heroNode的编号已然存在
                flag = true;//说明编号存在
                break;
            }
            temp = temp.next;//temp后移,遍历当前链表
        }
        //判断flag的值
        if (flag) {
            System.out.printf("准备插入节点的编号%d已经存在了,不能再插入\n", heroNode.no);
        } else//可以插入到链表中,在temp的后面
        {
            heroNode.next = temp.next;
            temp.next = heroNode;
        }
        //当退出while循环时,temp就指向了链表最后节点,将temp的next指向新的节点
        temp.next = heroNode;
    }

    //显示链表(遍历)
    public void show() {
        //判断链表是否为空
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        //因为head节点不能动,因此需要新建一个辅助变量temp
        HeroNode temp = head.next;
        while (true) {
            //判断是否到链表最后
            if (temp == null) {
                break;
            }
            //输出节点信息
            System.out.println(temp);
            //将temp后移
            temp = temp.next;
        }
    }

    //获取到单链表有效节点的个数(如果是带头节点的链表,需求不统计头节点)
    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;
    }
    //删除节点
    public void delete(int no) {
        //判断链表是否为空
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        //因为head节点不能动,因此需要新建一个辅助变量temp
        HeroNode temp = head.next;
        boolean flag = false;//flag标识是否找到需要删除节点的前一个节点,默认为false
        while (true) {
            //判断是否到链表最后
            if (temp.next == null) {
                break;
            }
            if (temp.next.no == no)//找到了需要删除节点的前一个节点
            {
                flag = true;
                break;
            }
            //将temp后移
            temp = temp.next;
        }
        if (flag) {
            temp.next = temp.next.next;
        } else {
            System.out.printf("需要删除节点编号%d不存在", no);
        }
    }
    //反转链表
    public static void Reverse(HeroNode head){
        //判断链表是否为空或者只有一个节点,此时无需反转,直接返回
        if (head.next == null){
            System.out.println("链表为空,无法反转");
        }
        if (head.next.next == null){
            System.out.println("链表只有一个节点,无法反转");
        }
        //获得链表有效节点个数
        int size = SingleLikedList.getLength(head);
        HeroNode temp = head.next;
        HeroNode next = null;//指向当前节点temp的下一个节点
        HeroNode reverseHead = new HeroNode(0,null,null);
        //从头到尾遍历原来的链表,每遍历一个节点,就将其取出,并放在新的链表reverseHead的最前端
        while (true){
            if(temp == null)//遍历到最后一个节点
            {
                break;
            }
            next = temp.next;//暂时保存当前节点的下一个节点
            temp.next = reverseHead.next; //将temp的下一个节点指向新的链表的最前端
            reverseHead.next = temp;
            temp = next;//遍历后移
        }
        head.next = reverseHead.next;
    }

}
//定义HeroNode类,每个HeroNode对象就是一个节点
class HeroNode {
    public int no;
    public String name;
    public String nickname;
    public HeroNode next;//指向下一个节点

    //构造器
    public HeroNode(int no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }

    //重写toString方法
    public String toString() {
        return "HeroNode[no=" + no + ",name=" + name + ",nickname=" + nickname + "]";
    }
}


3.3 运行结果

在这里插入图片描述

4、从尾到头打印单链表

4.1 解题思路

利用栈的先进后出特点,实现逆序打印的效果,未破坏链表结构。

4.2 代码编写

import java.util.Stack;

public class SingleLinedList_ReversePrint {
    public static void main(String[] args) {
        HeroNode hero1 = new HeroNode(1, "宋江", "及时雨");
        HeroNode hero2 = new HeroNode(2,"卢俊义","玉麒麟");
        HeroNode hero3 = new HeroNode(4,"吴用","智多星");
        HeroNode hero4 = new HeroNode(3,"林冲","豹子头");
        SingleLikedList singleLikedList = new SingleLikedList();
        singleLikedList.addByOrder(hero1);
        singleLikedList.addByOrder(hero2);
        singleLikedList.addByOrder(hero3);
        singleLikedList.addByOrder(hero4);
        System.out.println("====逆序打印前遍历链表信息====");
        singleLikedList.show();
        System.out.println("====逆序打印链表信息====");
        singleLikedList.ReversePrint(singleLikedList.getHead());
    }
}
//定义SingleLinkedList来管理英雄
class SingleLikedList {
    //先初始化一个头节点,头节点不要动,不存放具体数据
    private HeroNode head = new HeroNode(0, "", "");

    //返回头节点
    public HeroNode getHead() {
        return head;
    }

    public void addByOrder(HeroNode heroNode) {
        //因为head节点不能动,因此需要新建一个辅助变量temp,temp是位于添加位置的前一个节点
        HeroNode temp = head;
        boolean flag = false;//flag标识添加的编号是否存在,默认为false
        while (true) {
            if (temp.next == null) {//说明temp在链表最后
                break;
            }
            if (temp.next.no > heroNode.no) {//位置找到,就在temp后面插入
                break;
            } else if (temp.next.no == heroNode.no) {//说明希望添加的heroNode的编号已然存在
                flag = true;//说明编号存在
                break;
            }
            temp = temp.next;//temp后移,遍历当前链表
        }
        //判断flag的值
        if (flag) {
            System.out.printf("准备插入节点的编号%d已经存在了,不能再插入\n", heroNode.no);
        } else//可以插入到链表中,在temp的后面
        {
            heroNode.next = temp.next;
            temp.next = heroNode;
        }
        //当退出while循环时,temp就指向了链表最后节点,将temp的next指向新的节点
        temp.next = heroNode;
    }

    //显示链表(遍历)
    public void show() {
        //判断链表是否为空
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        //因为head节点不能动,因此需要新建一个辅助变量temp
        HeroNode temp = head.next;
        while (true) {
            //判断是否到链表最后
            if (temp == null) {
                break;
            }
            //输出节点信息
            System.out.println(temp);
            //将temp后移
            temp = temp.next;
        }
    }

    //逆序打印
    public static void ReversePrint(HeroNode head) {
        //判断链表是否为空,此时无需逆序打印,直接返回
        if (head.next == null) {
            System.out.println("链表为空,无需逆序打印");
            return;
        }
        Stack<HeroNode> heroNodes = new Stack<HeroNode>();
        HeroNode temp = head.next;
        while (temp != null){
            heroNodes.push(temp);
            temp = temp.next;
        }
        while (heroNodes.size() != 0){
            System.out.println(heroNodes.pop());
        }
    }
}
//定义HeroNode类,每个HeroNode对象就是一个节点
class HeroNode {
    public int no;
    public String name;
    public String nickname;
    public HeroNode next;//指向下一个节点

    //构造器
    public HeroNode(int no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }

    //重写toString方法
    public String toString() {
        return "HeroNode[no=" + no + ",name=" + name + ",nickname=" + nickname + "]";
    }
}

4.3 运行结果

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值