1. 开辟栈空间

/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 *	ListNode(int x) : val(x), next(nullptr) {}
 * };
 */
class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param pHead ListNode类 
     * @param k int整型 
     * @return ListNode类
     */
    ListNode* FindKthToTail(ListNode* pHead, int k) {
        
        // 开辟栈空间
        stack<ListNode*> st;
        int size = 0;
        while (pHead) {
            st.push(pHead);
            size++;
            pHead = pHead->next;
        }
        if (k > size) return nullptr;
        ListNode* res = nullptr;
        while (k--) {
            res = st.top();
            st.pop();
        }

        return res;
    }
};

2. 链表操作

/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 *	ListNode(int x) : val(x), next(nullptr) {}
 * };
 */
class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param pHead ListNode类 
     * @param k int整型 
     * @return ListNode类
     */
    ListNode* FindKthToTail(ListNode* pHead, int k) {
        /** 异常情况处理:
            1. pHead头节点为空
            2. k大于链表长度
            3. k值为0
        */
        if (pHead == nullptr || k <= 0) return nullptr;
        ListNode* fast = pHead;
        ListNode* slow = nullptr;
        for (int i = 0; i < k - 1; ++i) {
            if (fast->next) {
                fast = fast->next;
            } else {
                return nullptr;
            }
        }
        slow = pHead;
        while (fast->next) {
            slow = slow->next;
            fast = fast->next;
        }
        return slow;
    }
};