题目链接:https://www.acwing.com/problem/content/description/32/
题目如下:
/**
* 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) {
ListNode* pre=pListHead;
ListNode* cur=pListHead;
int size=0;
//统计链表节点数量和k的关系
while(cur!=NULL) {cur=cur->next;size++;}
if(size<k) return NULL;
cur=pListHead;
while(k--) cur=cur->next;
while(cur!=NULL){
pre=pre->next;
cur=cur->next;
}
return pre;
}
};