(双指针) $O(n)$
C++ 代码
class Solution {
public:
ListNode* findKthToTail(ListNode* pListHead, int k) {
if (pListHead == NULL) return NULL;
ListNode* dummy = new ListNode(-1);
dummy->next = pListHead;
ListNode* fast = dummy;
ListNode* slow = dummy;
while (k--) {
fast = fast->next;
if (fast == NULL) return NULL;
}
while (fast) {
fast = fast->next;
slow = slow->next;
}
return slow;
}
};