lets say u have 10 nodes 1->2->3->4->5->6->7->8->9->10 and u have to find 6th node from the end what this code is doing is its taking two pointers,current and behind...its frst moving current to 6 places...now current reaches to the 6th node after the frst loop...now u strt the second pointer ie behind from head and move both current and head until current reaches null....so if u c in this example current is pointing to 6th node right now it will move 4 places and reach the end and in the mean time behind will reach the 4th node...hence finally behind points to 4th node whch is 6th node from the end...i hope u get it...
On Mon, Aug 8, 2011 at 8:08 PM, sukran dhawan <[email protected]>wrote: > struct node * fun(struct node * list,int n) > > > > On Mon, Aug 8, 2011 at 8:06 PM, jagrati verma <[email protected] > > wrote: > >> hw is it possible frm this code ???????????/ >> >> >> >> >> Node * findNToLastNode( Node *head, int N ) >> { >> int i = 0; >> Node *current, *behind; >> current = head; >> for( i = 0; i < N; i++ ) { >> if( current->next ) { >> current = current->next; >> } else { >> return NULL; // Length of the list is less >> than N >> } >> } >> >> behind = head; >> while( current->next ) { >> current = current->next; >> behind = behind->next; >> } >> >> return behind; >> } >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Algorithm Geeks" group. >> To post to this group, send email to [email protected]. >> To unsubscribe from this group, send email to >> [email protected]. >> For more options, visit this group at >> http://groups.google.com/group/algogeeks?hl=en. >> >> > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > -- Aditi Garg Undergraduate Student Electronics & Communication Divison NETAJI SUBHAS INSTITUTE OF TECHNOLOGY Sector 3, Dwarka New Delhi -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
