题解 | #输出单向链表中倒数第k个结点#
输出单向链表中倒数第k个结点
http://www.nowcoder.com/practice/54404a78aec1435a81150f15f899417d
import java.util.*;
public class Main{ private static Node head; public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ int num = sc.nextInt(); head = new Node(); for(int i = 0; i < num;i++){ int val = sc.nextInt(); //指针指向下一个元素 head.next = new Node(val, head.next); } int target = sc.nextInt(); for(int i =0; i < target;i++){ //找到下一个元素 head = head.next; } System.out.println(head.value); } } } class Node{ Node node; int value; Node next; public Node(){
}
public Node(int value,Node next){
this.value = value;
this.next = next;
}
}