输入一个链表,输出该链表中倒数第k个结点。

/* public class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; } }*/
import java.util.Stack;
public class Solution {
   
    public ListNode FindKthToTail(ListNode head,int k) {
   
        Stack<ListNode> s=new Stack<ListNode>();
        ListNode pre=null;
        int count=0;
        if(head==null)//如果链表为空
            return null;
        while(head!=null){
   //将链表压入栈中,然后记录链表的结点个数
            s.push(head);
            head=head.next;
            count++;
        }
        if(k>count)//如果k>结点个数
            return null;
        for(int i=0;i<k;i++){
   
            pre=s.pop();
             
        }
        return pre;
    }
}