C++ function to return Nth the node from the end of the linked list in one pass.
Node * GetNthNode ( Node* Head , int NthNode )
{
Node * pNthNode = NULL;
Node * pTempNode = NULL;
int nCurrentElement = 0;
for ( pTempNode = Head; pTempNode != NULL; pTempNode = pTempNode->pNext )
{
nCurrentElement++;
if ( nCurrentElement - NthNode == 0 )
{
pNthNode = Head;
}
else
if ( nCurrentElement - NthNode > 0)
{
pNthNode = pNthNode - > pNext;
}
}
if (pNthNode )
{
return pNthNode;
}
else
return NULL;
}
Sunday, November 9, 2008
C++ function to return Nth the node from the end of the linked list in one pass.
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment