/*
struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { }};*/class Solution { public: ListNode* FindKthToTail(ListNode* pListHead, unsigned int k) { //变量定义区 ListNode* pNode = pListHead; ListNode* pPrevious = pListHead; //入参检查 if(pListHead == NULL || k ==0) return NULL; //先移动k个节点 for(unsigned int i=0; i < k-1; i++) { if(pNode->next == NULL ) return NULL; pNode = pNode->next; }//前后节点整体移动
while(pNode->next != NULL) { pNode = pNode->next; pPrevious = pPrevious->next; } return pPrevious; }};
程序已通过牛客网测试用例。