输入一个链表,输出该链表中倒数第k个结点。
代码:
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
typedef struct ListNode Node;
class Solution {
public:
ListNode* FindKthToTail(ListNode* pListHead, unsigned int k) {
Node* fast =pListHead;
Node* slow = pListHead;
while(k--)
{
if(fast==NULL)
return NULL;
fast = fast->next;
}
while(fast)
{
fast = fast->next;
slow = slow->next;
}
return slow;
}
};