输入一个链表,输出该链表中倒数第K个结点。
struct ListNode
{
int m_nValue;
ListNode* m_pNext;
};
ListNode* FindKthToTail(ListNode* pListHead , unsigned int K)
{
if (!pListHead || K <=)
{
return NULL ;
}
ListNode* pAhead = pListHead;
ListNode* pBehind = NULL ;
for (int i= ; i != K ;i++)
{
if (pAhead->m_pNext != NULL)
{
pAhead = pAhead->m_pNext ;
}
else
{
return NULL;
}
}
pBehind = pListHead ;
while(pAhead->m_pNext != NULL)
{
pAhead = pAhead->m_pNext ;
pBehind = pBehind->m_pNext;
}
return pBehind ;
}