时间限制:1秒 空间限制:32768K 热度指数:562607
本题知识点: 链表
算法知识视频讲解
题目描述
输入一个链表,输出该链表中倒数第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) {
if(pListHead==NULL||k==0)
return NULL;
vector<ListNode *> v;
while(pListHead){
v.push_back(pListHead);
pListHead=pListHead->next;
}
if(k>v.size())
return NULL;
return v[v.size()-k];
}
};