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

public class Solution {
    public ListNode FindKthToTail(ListNode head,int k) {
/**
有特殊情况:
1.链表为空或者返回倒数第0个节点
2.k要比链表中的节点数大
**/
        ListNode tail = head;

        if(head == null || k == 0){
            return null;
        }

        for(int i = 0; i < k-1; i++){

            if(tail.next!=null){
                tail = tail.next;
            }
            else{
                return null;
            }
        }

        while(tail.next != null){
            head = head.next;
            tail = tail.next;
        }

        return head;
    }
}