输入一个链表,输出该链表中倒数第K个结点。
1 struct ListNode 2 { 3 int m_nValue; 4 ListNode* m_pNext; 5 }; 6 ListNode* FindKthToTail(ListNode* pListHead , unsigned int K) 7 { 8 if (!pListHead || K <=0) 9 { 10 return NULL ; 11 } 12 ListNode* pAhead = pListHead; 13 ListNode* pBehind = NULL ; 14 for (int i=1 ; i != K ;i++) 15 { 16 if (pAhead->m_pNext != NULL) 17 { 18 pAhead = pAhead->m_pNext ; 19 } 20 else 21 { 22 return NULL; 23 } 24 } 25 pBehind = pListHead ; 26 while(pAhead->m_pNext != NULL) 27 { 28 pAhead = pAhead->m_pNext ; 29 pBehind = pBehind->m_pNext; 30 } 31 return pBehind ; 32 }