Java实现单链表有效节点个数、查找单链表中的倒数第k个节点、单链表反转、单链表逆序打印等4个常见单链表面试题

代码都是不含头结点的。

1、Java实现单链表的有效节点个数:

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

3.单链表反转

4.单链表逆序打印

----------------------------------------------------------------------------------------------------------------------

我把上面第四个常见面试题放在一个类里面,包含四个方法,并且在测试类中,调用方法进行测试

整体代码如下:

import javax.jws.WebParam;
import java.util.Stack;

public class LinkedList {
     Node first;//指向链表的第一个节点
     Node last;//指向链表的第二个节点


    public Node getFirst() {
        return first;
    }

    public void setFirst(Node first) {
        this.first = first;
    }

    public Node getLast() {
        return last;
    }

    public void setLast(Node last) {
        this.last = last;
    }

    /**
     * 首先判断链表是否为空
     *
     * @return
     */
    public boolean isEmpty() {
        return first == null;
    }

    /**
     * 将当前链表的内容打印出来
     */
    public void print() {
        //指向当前链表的头结点
        Node current = first;
        //开始从头结点也就是第一个节点开始遍历
        while (current != null) {
            System.out.println("["+current.data + " " + current.name + " " + current.np+"]");
            //下一个节点
            current = current.next;
        }
        System.out.println();
    }

    /**
     * 将指定节点插入到当前的链表中
     *
     * @param data 学生学号
     * @param np  学生成绩
  * @param name 学生姓名
     */
    public void insert(int data, String name, int np) {
        //创建一个新的节点对象
        Node newNode = new Node(data, name, np);
        if (this.isEmpty()) {
            //如果链表为空,新节点作为第一个节点,同时也是最后一个节点
            first = newNode;
            last = newNode;
        } else {
            //不为空的话
            last.next = newNode;
            last = newNode;
        }

    }

    /**
     * 单链表的有效节点个数
     *
     * @param first
     * @return
     */
    public static int getLength(Node first) {

        if (first == null) {
            return 0;
        }
        int length = 0;
        while (first != null) {
            length++;
            first = first.next;
        }
        return length;
    }

    /**
     * 查找单链表中的倒数第k个节点
     */
    public static Node findNode(Node first, int index) {
        //查找第k个节点
        if (first == null) {
            return null;
        }
        int size = getLength(first);
         if (index <= 0 || index > size){
            return null;
        }
        for (int i = 0; i < size - index ; i++) {
            first = first.next;
        }
        return first;
    }

    /**
     * 单链表反转
     */
    public static void reverseList(Node first){
        //如果链表是空的或者只有一个,反转还是其本身
        Node current = first;
        Node before = null;
        Node last = null;
        if (current == null || current.next == null){
            return ;
        }
        while (current != null) {
            last = before;
            before = current;
            current = current.next;
            before.next = last;
        }
        current = before;
        while (current != null) {
            System.out.println("["+current.data+" "+current.name+" "+
                    current.np+"]");
            current = current.next;
        }
        System.out.println();

/**
     * 单链表逆序打印
     */
    public static  void  lastToBegin(Node first){
        Stack<Node> stack = new Stack();
        Node pop = null;
        if (first == null ){
            return ;
        }
        while (first != null) {
            stack.push(first);
            first = first.next;
        }
        //出栈
        while (stack.size() > 0) {
            System.out.println( pop = stack.pop());
        }

    }
}
/**
 * 定义节点和数据
 */
class Node {
    int data;
    int np;
    String name;
    Node next;

    public Node(int data, String name, int np) {
        this.data = data;
        this.np = np;
        this.name = name;
        this.next = null;
    }

    @Override
    public String toString() {
        return "Node{" +
                "data=" + data +
                ", np=" + np +
                ", name='" + name + '\''  +
                '}';
    }
}

测试代码如下:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Tetst {
    public static void main(String[] args) throws IOException {

        BufferedReader buf;
        buf = new BufferedReader(new InputStreamReader(System.in));
        int num;
        String name;
        int score;
        System.out.println("请输入5位学生的数据:");
        LinkedList list = new LinkedList();
        for (int i = 0; i < 5; i++) {
            System.out.println("请输入学号:");
            num = Integer.parseInt(buf.readLine());

            System.out.println("请输入姓名:");
            name = buf.readLine();

            System.out.println("请输入成绩:");
            score = Integer.parseInt(buf.readLine());

            list.insert(num,name,score);

            System.out.println("-----------");

        }
        System.out.println("学生学号"+"  "+"学生姓名"+"  "+"学生成绩");
        list.print();
        int length = LinkedList.getLength(list.getFirst());
        Node node = LinkedList.findNode(list.getFirst(), 2);
        System.out.println(node);
        System.out.println(length);


        LinkedList.lastToBegin(list.getFirst());
        System.out.println("原来链表的情况:");
        list.print();
        System.out.println("反转后的情况:");
        LinkedList.reverseList(list.getFirst());

    }
}

测试结果:

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阳光不锈@

如果有帮助的话,打赏一下吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值