1 /**
2 * struct ListNode {
3 * int val;
4 * struct ListNode *next;
5 * ListNode(int x) : val(x), next(nullptr) {}
6 * };
7 */
8 class Solution {
9 public:
10 /**
11 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
12 *
13 *
14 * @param pHead ListNode类
15 * @param k int整型
16 * @return ListNode类
17 */
18 ListNode* FindKthToTail(ListNode* pHead, int k) {
19 /*
20
21 int count=0;
22 map mListNodeValue;
23 // write code here
24 if(pHead==NULL)
25 {
26 return NULL;
27 }
28 ListNode* q=pHead;
29 while(q!=NULL)
30 {
31 mListNodeValue[count++]=q;
32
33 q=q->next;
34 }
35 if(k>count)
36 {
37 return NULL;
38 }
39 return mListNodeValue[count-k];
40 */
41
42
43
44 ListNode* p;
45 ListNode* q;
46 p=pHead;
47 q=pHead;
48 if(pHead==NULL)
49 {
50 return NULL;
51 }
52
53 for(int i=0;i)
54 {
55 if(p==NULL)
56 {
57 return NULL;
58 }
59 p=p->next;
60
61 }
62
63 while(p!=NULL&&q!=NULL)
64 {
65 p=p->next;
66 q=q->next;
67 }
68 return q;
69 }
70 };