题目是给定一个链表:
struct ListNode { int m_nKey; ListNode* m_pNext; };
输入一个单向链表,输出该链表中倒数第k个结点,链表的倒数第1个结点为链表的尾指针。
* 输入描述:
* 输入说明
* 1 输入链表结点个数
* 2 输入链表的值
* 3 输入k的值
*
* 输出描述:
* 输出一个整数
public static void main(String[] args) throws Exception { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { int length = scanner.nextInt(); ListNode head = new ListNode(); for (int i = length; i > 0; i--) { int key = scanner.nextInt(); ListNode newListNode = new ListNode(key, head.next); head.next = newListNode; } int index = scanner.nextInt(); for (int i = 0; i < index; i++) { head = head.next; } System.out.println(head.value); } } } class ListNode { int value; ListNode next; ListNode() { } ListNode(int value, ListNode next) { this.value = value; this.next = next; } }