题解 | #输出单向链表中倒数第k个结点#
输出单向链表中倒数第k个结点
https://www.nowcoder.com/practice/54404a78aec1435a81150f15f899417d
import java.util.*; import java.io.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); try { String s1; //输入链表结点个数 while ((s1 = br.readLine()) != null) { String s2 = br.readLine(); //输入链表的值 String s3 = br.readLine(); //输入k的值 String[] s = s2.split(" "); Node head = new Node(Integer.valueOf(s[0]), null); Node tail = head; for (int i = 1; i < s.length; i++) { tail.next = new Node(Integer.valueOf(s[i]), null); tail = tail.next; } int a = Integer.valueOf(s1) - Integer.valueOf(s3); while (a-- != 0) { head = head.next; } System.out.println(head.val); } } catch (IOException e) { e.printStackTrace(); } } public static class Node { int val; Node next; public Node(int val, Node next) { this.val = val; this.next = next; } } }