题解 | #输出单向链表中倒数第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 scanner = new Scanner(System.in);
while (scanner.hasNext()) {
int length = scanner.nextInt();
ListNode header = new ListNode(-1);
ListNode temp = header;
for (int i = 0; i < length; i++) {
int value = scanner.nextInt();
ListNode node = new ListNode(value);
temp.next = node;
temp = node;
}
int key = scanner.nextInt();
for (int i = 0; i < length - key + 1; i++) {
header = header.next;
}
System.out.println(header.value);
}
}
}
class ListNode {
int value;
ListNode next;
ListNode(int value) {
this.value = value;
}
}
查看6道真题和解析
海康威视公司福利 1125人发布