title: How to reverse linked list C++
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/1801549/reverse-a-singly-linked-list">Reverse a singly linked list</a><br>
    <a href="https://stackoverflow.com/questions/2887600/reverse-a-linked-list">reverse a linked list?</a>  </p>
  </blockquote>
  
  
  
  <p>I have been trying to figure out how to displays the contents of a linked list of numbers in reverse order. I tried changing nodes around and i cant figure it out. its a singly linked list. I thought of implementing a stack. is there a simpler way? heres my code. i need the function to be in the header file, and it will be implemented in the main.cpp. ive been trying for hours today and yesterday and this is really a last resort. i have pages of notes of failed algorithms. as well as pages of google searches but nothing seems to work. if it makes a difference im using Microsoft Visual Studio 2010 (not my first choice). the code i presented does not have what ive been trying as it all failed horribly and usually produced errors. </p>
  
  <pre><code>#ifndef _LINKEDLIST_H
  #define _LINKEDLIST_H
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;stack&gt;
  
  #include "Node.h"
  
  using namespace std;
  using namespace bruno;
  
  namespace bruno
  {
  template&lt; typename T&gt;
  class LinkedList
  {
      private:        Node&lt;T&gt;  * head;
                      Node&lt;T&gt;  * tail;
                      int numberOfNodes;
  
      public:         LinkedList();
                      ~LinkedList();
  
                      int length(); 
  
                      Node&lt;T&gt;  * getHead() { return head; };
                      Node&lt;T&gt;  * getTail() { return tail; };
  
                      void insertInFront(T d);
                      void insertInBack(T d);
  
                      static void listContents( Node&lt;T&gt; * head);  
  
                      static T  sum( Node&lt;T&gt;  * head);    //     assume head is not NULL at call
  
                      static void  increment( Node&lt;T&gt;  * head, T val); 
                      static void reverseListContents( Node&lt;T&gt; * head, Node&lt;T&gt; * tail, int n );
  };
  
  
     template&lt; typename T &gt;
     LinkedList&lt;T&gt;::LinkedList()
     {
         head = tail = NULL;
         numberOfNodes = 0;
     }
  
  
     template&lt; typename T &gt;
     LinkedList&lt;T&gt;::~LinkedList()
     {
     }
  
  
  
     template&lt; typename T &gt;
     int LinkedList&lt;T&gt;::length()
     { 
         return numberOfNodes; 
     }
  
  
  
     template&lt; typename T &gt;
     void LinkedList&lt;T&gt;::insertInFront(T d)
     {
          Node&lt;T&gt; * p =  new Node&lt;T&gt;;
          p-&gt;data  =  d;
          p-&gt;next  = head;
          head = p;
          if ( tail == NULL )
              tail = p;
          numberOfNodes++;
     }
  
  
  
  
     template&lt; typename T &gt;
     void LinkedList&lt;T&gt;::insertInBack(T d)
     {
          if ( tail == NULL )
              insertInFront(d);
          else {
                  tail-&gt;next = new Node&lt;T&gt;;
                  tail = tail-&gt;next;
                  tail -&gt; data = d;
                  tail -&gt; next = NULL;
          }
          numberOfNodes++;
  
     }
  
  
     template&lt; typename T &gt;                       
     void LinkedList&lt;T&gt;::listContents(Node&lt;T&gt; * head)
     {
          if (head == NULL)  return;
          else
          {      cout &lt;&lt;  head-&gt;data  &lt;&lt; "  -&gt;  ";
                 listContents( head -&gt; next );
          }
     }
  
  
  
  
     template&lt; typename T &gt;                       
     T LinkedList&lt;T&gt;::sum(Node&lt;T&gt; * head)
     {
          if ( (head-&gt;next) == NULL )  return  head-&gt;data;
          else
               return  ( head-&gt;data    +   sum( head-&gt;next ) );
     }
  
  
  
  
     template&lt; typename T &gt;                       
     void LinkedList&lt;T&gt;::increment(Node&lt;T&gt; * head, T  val)
     {
          if ( head == NULL )  return;
          else
          {
              head-&gt;data += val;                    // add val to current data value
              increment( head-&gt;next, val );
          }
  
     }
  
     template&lt; typename T &gt;
     void LinkedList&lt;T&gt;::reverseListContents (Node&lt;T&gt; * head, Node&lt;T&gt; * tail, int n)
     {
         //clueless!
  
  
     }
  
  
  
  }
  
  #endif 
  </code></pre>