/** * struct ListNode { * int val; * struct ListNode *next; * }; */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param pHead ListNode类 * @param k int整型 * @return ListNode类 */ struct ListNode* FindKthToTail(struct ListNode* pHead, int k ) { struct ListNode* p; p = pHead; int i = 0; while(p != NULL) { i++; p = p->next; } if(i<k) return NULL; for(int j=0;j<i-k;j++) { pHead = pHead->next; } return pHead; }
先算出链表长度,然后偏移到指定位置,传出地址。