题解 | #输出单向链表中倒数第k个结点#
输出单向链表中倒数第k个结点
https://www.nowcoder.com/practice/54404a78aec1435a81150f15f899417d
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int num = sc.nextInt(); int[] ints = new int[num]; for (int i = 0; i < num; i++) { ints[i] = sc.nextInt(); } int key = sc.nextInt(); for (int j = 1, i = ints.length - 1; i >= 0 ; i--) { if (j++ == key) { System.out.println(ints[i]); break; } } } sc.close(); } }