输入一个链表,输出该链表中倒数第k个结点。
1 /* 2 struct ListNode { 3 int val; 4 struct ListNode *next; 5 ListNode(int x) : 6 val(x), next(NULL) { 7 } 8 };*/ 9 class Solution { 10 public: 11 ListNode* FindKthToTail(ListNode* pListHead, unsigned int k) { 12 ListNode* p = pListHead; 13 for(int i = 0; i < k; i++){ 14 if(p == NULL) 15 return NULL; 16 else 17 p = p->next; 18 } 19 while(p!=NULL){ 20 p = p->next; 21 pListHead = pListHead->next; 22 } 23 return pListHead; 24 } 25 };