题目描述
输入一个链表,输出该链表中倒数第k个结点。
链接
代码
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* FindKthToTail(ListNode* pListHead, unsigned int k) {
int len = 0;
ListNode* ptemp = pListHead;
if(ptemp == NULL){
return NULL;
}
while(ptemp){
len++;
ptemp = ptemp->next;
}
ptemp = pListHead;
if(len < k){
return NULL;
}
else{
for(int i = 0; i < len-k; i++){
ptemp = ptemp->next;
}
return ptemp;
}
}
};