title: Reverse Linked List in C++ Using Only Node Pointer
body: <p>My teacher left this task:  </p>
  
  <blockquote>
    <p>Reverse the order of the elements of a linked list, only by manipulating pointers in each node. It is not allowed to move the item as such, nor is to create a new node for this operation.</p>
  </blockquote>
  
  <p>I've tried to think in a solution for this but everytime I end up needing to create a new node pointer or calling another method that returns a created one.</p>
  
  <p>Here's some code, the list is singly linked, "first" is the head of the list and "last" is the last pointer of the list.</p>
  
  <p>My method for getting a node pointer:</p>
  
  <pre><code>template &lt;class T&gt;
  Node&lt;T&gt;* LinkedList&lt;T&gt;::getNode(unsigned int i)
  {
      int index = 0;
      if (i == 0)
          return first;
      Node&lt;T&gt;* cursor = first;
      while (index != i &amp;&amp; cursor)
      {
          cursor = cursor-&gt;getNext();
          ++index;
      }
      return cursor;
  }
  </code></pre>
  
  <p>And, here's my method for reverse the list:</p>
  
  <pre><code>template &lt;class T&gt;
  void LinkedList&lt;T&gt;::reverse()
  {
      int i = 1, j = 2;
      while(j &lt;= size)
      {
          getNode(size - 1)-&gt;setNext(getNode(size - j++));
          if (j == size + 1)
              first = getNode(size - (j - 2));
          else
              getNode(size - j)-&gt;setNext(getNode(size - i++));
          getNode(size - 1)-&gt;setNext(nullptr);
      }
      last = getNode(size - 1);
  }
  </code></pre>
  
  <p>As I said before, I needed to know if there's a way for me to do the same without using that get method (which creates a node pointer). I think that when he refers to create a new node he's talking about a node pointer because you can't create a node (an object not a pointer) and assign to it a pointer of the list.</p>
  
  <p>IF there's a way I'd be really gratefull if someone shares it, and if there's no way then I'll do it the normal way. Thanks to the people who has answered.</p>