Print a singly linked list in reverse!

Today’s topic is pretty simple. Let’s print a linked list in reverse. And it is a singly linked list meaning you can only traverse in one direction and not come back. You are only given a pointer to the head. So without further ado, below is the c++ code. Only the important parts are shown. Rest is left as an exercise for the reader to create a fully working program.

/*
 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;
 }
*/
#include < vector >

void ReversePrint(Node *head)
{
 // This is a "method-only" submission. 
 // You only need to complete this method. 
 vector<int> data{0};
 while(head) {
   data.push_back(head->data);
   if(head->next) {
     head = head->next;
   } else { break; }
 }
 for(int i = data.size() - 1; i > 0; i--) {
   cout << data[i] << endl;
 }
}

The only thing I have done here is to use vector. On the forward traversal, simply push the values in and reverse traverse the list to print it. There are tons of others ways to do it. What is the other simple way?

Of course yes, it is using a stack. Simply push into it. And then start popping out once the traversal is done till the stack is empty. ENJOY the code!