/** * struct ListNode { * int val; * struct ListNode *next; * }; */ /** * * @param pListHead ListNode类 * @param k int整型 * @return ListNode类 */ struct ListNode* FindKthToTail(struct ListNode* pListHead, int k ) { // write code here if(pListHead==NULL) return NULL; struct ListNode*slow=pListHead; struct ListNode*fast=pListHead; while(k!=0) { if(fast==NULL) { return NULL; } fast=fast->next; k--; } while(fast) { slow=slow->next; fast=fast->next; } return slow; }
快慢指针