题目描述
输入一个链表,输出该链表中倒数第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,* pre = pListHead; 13 while(k-- && p != NULL) 14 p = p->next; 15 if (k!=-1) 16 return NULL; 17 while(p != NULL) 18 { 19 p = p->next; 20 pre = pre->next; 21 } 22 return pre; 23 } 24 };