/** 1.先统计链表结点个数 2.查找倒数的那个结点 3.释放之前无用的节点 4.将节点指针赋值给头指针 * struct ListNode { * int val; * struct ListNode *next; * }; * * C语言声明定义全局变量请加上static,防止重复定义 */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param pHead ListNode类 * @param k int整型 * @return ListNode类 */ struct ListNode* FindKthToTail(struct ListNode* pHead, int k ) { // write code here /*统计链表结点个数*/ struct ListNode* p = pHead; struct ListNode* t = pHead; struct ListNode* temp; struct ListNode* save; int count = 0; int ListLength = 0; if(pHead == NULL) return pHead; while(p != NULL){ ListLength++; p = p->next; } /*如果k值大于链表结点数,就返回NULL*/ if(k > ListLength){ return NULL; } ListLength = ListLength-k; while(count != ListLength){ count++; save = t; t = t->next; free(save); /*释放结点*/ } pHead = t; return pHead; }