C++/代码

class Solution {
public:
    ListNode* FindKthToTail(ListNode* pListHead, unsigned int k) {
        int n = 0;
        for (auto p = pListHead; p; p = p->next) n ++; //求出链表的长度,p不为空时,继续
        if (k > n) return nullptr; //k大于n,非法情况
        for (int i = 0; i < n - k; i ++) pListHead = pListHead->next; //求出n-k长度的数值,就是倒数第k个节点
        return pListHead;
    }
};