C数据结构面试题精选

1、求链表中倒数第K个节点

ListNode* FindKthToTail(ListNode* pListHead, unsigned int k)
{
        if(pListHead == NULL || k == 0)
                return NULL;
 
        ListNode *pAhead = pListHead;
        ListNode *pBehind = NULL;
 
        for(unsigned int i = 0; i < k - 1; ++ i)
        {
                pAhead = pAhead->m_pNext;
		if(pAhead == NULL)
			return NULL;
        }
 
        pBehind = pListHead;
 
        while(pAhead->m_pNext != NULL)
        {
                pAhead = pAhead->m_pNext;
                pBehind = pBehind->m_pNext;
        }
 
        return pBehind;
}

要注意一些错误处理机制;

转自:http://blog.csdn.net/cadcisdhht/article/details/6197041

你可能感兴趣的:(C数据结构面试题精选)