题解 | #输出单向链表中倒数第k个结点#
https://www.nowcoder.com/practice/54404a78aec1435a81150f15f899417d
快慢指针 import java.util.*; import java.io.*; import java.lang.Math; public class Main { public static void main(String[]args){ Scanner in=new Scanner(System.in); while(in.hasNext()){ int n=in.nextInt(); ListNode node=new ListNode(-1); ListNode head=node; for(int i=0;i<n;i++){ int value=in.nextInt(); ListNode temp=new ListNode(value); node.next=temp; node=node.next; } int reverse=in.nextInt(); ListNode left=head; ListNode right=head; for(int i=0;i<reverse;i++){ right=right.next; } while(right!=null){ left=left.next; right=right.next; } System.out.println(left.value); } } } class ListNode { int value; ListNode next; public ListNode () { } public ListNode (int value, ListNode next) { this.value = value; this.next = next; } public ListNode (int value) { this.value = value; next = null; } }