(个人水平有限,请见谅!)
题目描述:
输入一个链表,输出该链表中倒数第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 *p , *q;
p = q = pListHead;
if (pListHead==NULL || k<=0)
return NULL;
for (int i = 0; i < k; i++)
{
if (p != NULL)
p = p->next;
else
return NULL;
}
while (p != NULL)
{
p = p->next;
q = q->next;
}
return q;
}
};