目录
核心:看图
struct ListNode* FindKthToTail(struct ListNode* pListHead, int k ) {
// write code here
// write code here
if (k == 0)
{
return NULL;
}
else
{
struct ListNode* slow = pListHead, * fast = pListHead;
//fast先走k步数
while (k--)
{
if (fast == NULL)
return NULL;
fast = fast->next;
}
//slow和fast一起走
while (fast)
{
fast = fast->next;
slow = slow->next;
}
return slow;
}
}