day6 HJ51 输出单向链表中倒数第k个结点

101 阅读1分钟

题目来源: HJ51 输出单向链表中倒数第k个结点

题目描述:

image.png

具体实现:

 import java.util.Scanner;
 public class Main {
     public static void main(String[] args) {
         // 输入
         Scanner in = new Scanner(System.in);
         while(in.hasNext()) {
             int n =  in.nextInt();
             MyLinkedList list = new MyLinkedList();
             for (int i = 0; i< n;i++) {
                 list.add(in.nextInt());
             }
             int k = in.nextInt();
             // 输出
             System.out.println(list.get(n - k));
         }
         // 关闭
         in.close();
     }
 }
 class Node {
     public int data;
     public Node next;
     public Node(int data) {
         this.data = data;
     }
 }
 class MyLinkedList {
     // 实际链表里面的元素个数
     private int size = 0;
     // 头节点
     private Node first;
     // 尾节点
     private Node last;
     public MyLinkedList() {
     }
     public boolean add(int e) {
         final Node l = last;
         // 构造一个新节点
         final Node  newNode = new Node (e);
         last = newNode;
         // 判断尾节点,尾节点为null,则证明链表是空的。
         // 如果链表是空的,新增加的节点就作为头结点;
         // 如果链表是不空,则原尾节点的next指向新增加的节点
         if (l == null) {
             first = newNode;
             last = newNode;
         } else {
             l.next = newNode;
         }
         size++; // size累加1位
         return true;
     }
     public int get(int index) {
         // 判断是否越界
         if (index < 0 || index > size - 1) {
             throw new IndexOutOfBoundsException(
                     "index " + index + " out of bounds");
         }
         Node x = first;
         // 遍历链表
         for (int i = 0; i < index; i++) {
             x = x.next;
         }
         return x.data;
     }
 }