输入一个链表,输出该链表中倒数第k个结点。
C++
/*
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 count=0;
ListNode* p=pListHead;
while(p)
{
count++;
p=p->next;
}
int m=count-k+1;
int n=0;
ListNode* q=pListHead;
while(q)
{
n++;
if(m==n)
{
break;
}
q=q->next;
}
return q;
}
};