链表的倒数第k个节点一般采用快慢指针实现查询效率为n的方法。
插入采用尾插法,记录尾指针

import java.util.*;
public class Main{
    public static class ListNode{
        int val;
        ListNode next;
        ListNode(int val){
            this.val = val;
        }
    }
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n = sc.nextInt();
            ListNode head = new ListNode(sc.nextInt());
            ListNode rear = head;
            for(int i = 1;i<n;i++){
                ListNode listNode = new ListNode(sc.nextInt());
                rear.next = listNode;
                rear = rear.next;
            }
            int k = sc.nextInt();
            if(k>n||k<1){
                System.out.println(0);
            }
            else{
                ListNode fast = head;
                ListNode slow = head;
                for(int i = 1;i<k;i++){
                    fast = fast.next;
                }
                while(fast.next!=null)
                {
                    fast = fast.next;
                    slow = slow.next;
                }
                 System.out.println(slow.val);
            }
        }
    }
}