Tutorials, Free Online Tutorials,It Challengers provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, core java, sql, php, c language etc. for beginners and professionals.

Breaking

13.Return Nth the node from the end of the linked list in one pass.


13.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;
}

No comments:

Post a Comment