oral_quiz->#求链表中的倒数第K个结点#

#include "myfuncs.h"
template <typename ListNode>
ListNode* FindKthToTail(ListNode* pListHead, unsigned int k) {
	if(pListHead == NULL) return NULL;
	Assert(k > 0, "k must larger than zero");

	ListNode *pAhead = pListHead;
	ListNode *pBehind = NULL;

	for(unsigned int i=0; i<k-1; ++i) {
		Assert(pAhead->m_pNext != NULL, "k is longer than the list's length");
		pAhead = pAhead->m_pNext;
	}

	pBehind = pListHead;

	while(pAhead->m_pNext != NULL) {
		pAhead = pAhead->m_pNext;
		pBehind = pBehind->m_pNext;
	}

	return pBehind;
}



你可能感兴趣的:(oral_quiz->#求链表中的倒数第K个结点#)