Idea: set two pointers, a slow pointer pSlow, a fast pointer pFast, fast pointer first K-1 step, then two pointers at the same time, when the fast pointer to the end of the chain table, the slow Pointer Points to the nearest k node.
Special case: If k is greater than the length of the linked list, the header node is returned.
Code:
# Include "stdafx. h "# include <iostream> using namespace std; struct ListNode {int m_nValue; ListNode * m_pNext ;}; ListNode * FindKthToTail (ListNode * pHead, int k) {if (pHead = NULL | k = 0) {return NULL;} ListNode * pSlow = pHead; ListNode * pFast = pHead; int I = 1; while (I <k) {pFast = pFast-> m_pNext; if (pFast = NULL) {cout <"k =" <k <"greater than the length of the linked list! "<Endl; break;} I ++;} if (pFast! = NULL) {while (pFast-> m_pNext! = NULL) {pSlow = pSlow-> m_pNext; pFast = pFast-> m_pNext;} return pSlow;} // create a linked list and enter the value of the start and end nodes, input-1 to end void CreateList (ListNode * & pHead) {ListNode * pListNode = NULL; ListNode * pCurLastNode = NULL; bool isHead = true; while (1) {if (isHead) {pHead = new ListNode (); cin> pHead-> m_nValue; pHead-> m_pNext = NULL; isHead = false; pCurLastNode = pHead ;} else {pListNode = new ListNode (); cin> PListNode-> m_nValue; if (pListNode-> m_nValue =-1) {break;} pListNode-> m_pNext = NULL; pCurLastNode-> m_pNext = pListNode; pCurLastNode = pListNode ;}}// print the void PrintList (ListNode * & pHead) {if (pHead! = NULL) {ListNode * pCur = pHead; while (pCur! = NULL) {cout <pCur-> m_nValue <""; pCur = pCur-> m_pNext;} cout <endl;} else {cout <"linked list is empty! "<Endl ;}} int _ tmain (int argc, _ TCHAR * argv []) {ListNode * pListHead = NULL; CreateList (pListHead); PrintList (pListHead ); listNode * pKthNodeToTail = FindKthToTail (pListHead, 6); cout <pKthNodeToTail-> m_nValue <endl; return 0 ;}# include "stdafx. h "# include <iostream> using namespace std; struct ListNode {int m_nValue; ListNode * m_pNext;}; ListNode * FindKthToTail (ListNode * pHead, int k) {if (pHead = NULL | k = 0) {return NULL;} ListNode * pSlow = pHead; ListNode * pFast = pHead; int I = 1; while (I <k) {pFast = pFast-> m_pNext; if (pFast = NULL) {cout <"k =" <k <"greater than the length of the linked list! "<Endl; break;} I ++;} if (pFast! = NULL) {while (pFast-> m_pNext! = NULL) {pSlow = pSlow-> m_pNext; pFast = pFast-> m_pNext;} return pSlow;} // create a linked list and enter the value of the start and end nodes, input-1 to end void CreateList (ListNode * & pHead) {ListNode * pListNode = NULL; ListNode * pCurLastNode = NULL; bool isHead = true; while (1) {if (isHead) {pHead = new ListNode (); cin> pHead-> m_nValue; pHead-> m_pNext = NULL; isHead = false; pCurLastNode = pHead ;} else {pListNode = new ListNode (); cin> pListNode-> m_nValue; if (pL IstNode-> m_nValue =-1) {break;} pListNode-> m_pNext = NULL; pCurLastNode-> m_pNext = pListNode; pCurLastNode = pListNode ;}}} // print the linked list void PrintList (ListNode * & pHead) {if (pHead! = NULL) {ListNode * pCur = pHead; while (pCur! = NULL) {cout <pCur-> m_nValue <""; pCur = pCur-> m_pNext;} cout <endl;} else {cout <"linked list is empty! "<Endl ;}} int _ tmain (int argc, _ TCHAR * argv []) {ListNode * pListHead = NULL; CreateList (pListHead); PrintList (pListHead ); listNode * pKthNodeToTail = FindKthToTail (pListHead, 6); cout <pKthNodeToTail-> m_nValue <endl; return 0 ;}