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