题目简介
1->2->3->4->5->6
输出:4
提示: 可以运用快慢指针
牛客
/*
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* fast = pListHead;
ListNode* slow = pListHead;
ListNode* ctl = pListHead;
int count = 0;
if(pListHead == NULL)
return NULL;
while(ctl != NULL)
{
ctl = ctl->next;
count ++;
}
if(count < k)
return NULL;
while(k--)
{
fast = fast->next;
}
while(fast != NULL)
{
slow = slow->next;
fast = fast->next;
}
return slow;
}
};