比价常规的思路
C++ 代码
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* findKthToTail(ListNode* pListHead, int k) {
if(k<=0||!pListHead){
return pListHead;
}
ListNode* fast=pListHead;
int num=k;
while(fast&&(num-->0)){
fast=fast->next;
}
if(num>0){
return NULL;
}
ListNode* slow=pListHead;
while(fast){
slow=slow->next;
fast=fast->next;
}
return slow;
}
};