题目
链表中倒数第k个结点
时间限制:1秒 空间限制:32768K 热度指数:432979
算法知识视频讲解
题目描述
输入一个链表,输出该链表中倒数第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)
{
ListNode* first=NULL;
ListNode* second=NULL;
if(pListHead==NULL)
return second;
int count=0;
first=pListHead;
second=pListHead;
while(first)
{
first=first->next;
if(count>=k)
second=second->next;
count++;
}
if(k>count)
second=NULL;
return second;
}
};