题解 | #输出单向链表中倒数第k个结点#
输出单向链表中倒数第k个结点
http://www.nowcoder.com/practice/54404a78aec1435a81150f15f899417d
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while(in.hasNextInt()){
int n = in.nextInt();
int[] data = new int[n];
for(int i = 0; i<n; i++){
data[i] = in.nextInt();
}
int rearIndex = in.nextInt();
ListNode head = new ListNode(0,null);
for(int i = 0; i<n; i++){
ListNode node = head;
while(node.next != null){
node = node.next;
}
node.next = new ListNode(data[i],null);
}
ListNode no = head.next;
if(rearIndex==0){
System.out.println(0);
}else{
for(int j = 0; j<n-rearIndex; j++){
no = no.next;
}
System.out.println(no.key);
}
}
}
}
class ListNode{
int key;
ListNode next;
ListNode(int key, ListNode n){
this.key = key;
this.next = n;
}
}