链表中的倒数第k个节点
题目描述
输入一个链表,输出该链表中倒数第k个结点。
/* struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { } };*/ class Solution { public: ListNode* FindKthToTail(ListNode* pListHead, unsigned int k) { ListNode* pre = pListHead; ListNode* back = pListHead; int count = 0; while(pre != nullptr) { if(count < k) ++count; else back = back->next; pre = pre->next; } return count < k ? nullptr : back; } };