Print a linked list in Reverse solution

Print a linked list in Reverse solution


Problem Statement

You're given the pointer to the head node of a linked list and you need to print all its elements in reverse order from tail to head, one element per line. The head pointer may be null meaning that the list is empty - in that case, don't print anything!

Soruce code:

 /*  
  Print elements of a linked list in reverse order as standard output  
  head pointer could be NULL as well for empty list  
  Node is defined as   
  struct Node  
  {  
    int data;  
    struct Node *next;  
  }  
 */  
 void ReversePrint(Node *head)  
 {  
  // This is a "method-only" submission.   
  // You only need to complete this method.   
   int a[100],i=0;  
   while(head!=NULL){  
     a[i] = head->data;  
     i++;  
     head = head->next;  
   }  
   for(int j = i-1;j>=0;j--){  
     cout<<a[j]<<endl;  
   }  
 }  

7 comments :

  1. This comment has been removed by the author.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. Many printers have a feature that reverses the output. The problem is there doesn’t seem to be a consistent name for this feature.Some names are given like flip horizontal
    ,mirror image,reverse or emulsion down.The given source code is very helpful in creating a back print.

    ReplyDelete
  6. I like your professional advice! you are an example for me. keep going! follow writing essays custom and improve youself as well as your site!

    ReplyDelete
  7. u did well but linkedlist means we should allocate memory in dynamic location but u used arrays ,what happen if give more than 100 numbers??

    ReplyDelete