1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* findKthToTail(ListNode* pListHead, int k) {
int fcnt = 0,skip = 0;
ListNode * p = pListHead;
int K = k;
while(k--) {
if(p==NULL) break;
p = p->next;
skip++;
}
// printf("skip:=%d,k:=%d\n",skip,k);
if(skip < K) return NULL;
//printf("skip:=%d,k:=%d\n",skip,k);
ListNode *q = pListHead;
while(p) {
p = p->next;
q = q->next;
}
//cout << q ->val <<endl;
return q;
}
};
|