Advertisement
Guest User

Mth to last Element of a Linked List Challenge

a guest
Jun 23rd, 2011
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.30 KB | None | 0 0
  1. Node *findMToLastNode( Node *head, int m){
  2.     Node *cur=head;
  3.     Node *mBehind=NULL;
  4.    
  5.     int i;
  6.     for (i=0;i<m;i++){
  7.         if (cur->next){
  8.             cur=cur->next;
  9.         }
  10.         else{
  11.             return mBehind;
  12.         }
  13.     }
  14.    
  15.     mBehind=head;
  16.    
  17.     while(cur->next){
  18.         cur=cur->next;
  19.         mBehind=mBehind->next;
  20.     }
  21.    
  22.     return mBehind;
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement