title: C Pointers are Reverting to Old Address in Recursion
body: <p>There are 3 linked lists, 2 of which (headX and headY) are given in descending order, and the functions job is to sort them into a single list (headZ).</p>
  
  <p>For some reason, the algorithm works fine, but when its done and its going back through the recursion, the 3 pointers revert to their old values as they go "back" through the recursion.</p>
  
  <pre><code>void SortedMergeRecur(Node* headX, Node* headY, Node* headZ)
  {
      if (headX == NULL &amp;&amp; headY == NULL)
          return;
  
  else if (headX == NULL &amp;&amp; headY != NULL)
  {
      if (headZ == 0)
      {
          headZ = headY;
          headY = headY-&gt;link;
          headZ-&gt;link = NULL;
      }
      else
      {
          headZ-&gt;link = headY;
          headY = headY-&gt;link;
          headZ = headZ-&gt;link;
          headZ-&gt;link = NULL;
      }
  
      SortedMergeRecur(headX, headY, headZ);
  }
  
  else if (headX != NULL &amp;&amp; headY == NULL)
  {
      if (headZ == 0)
      {
          headZ = headX;
          headX = headX-&gt;link;
          headZ-&gt;link = NULL;
      }
      else
      {
          headZ-&gt;link = headX;
          headX = headX-&gt;link;
          headZ = headZ-&gt;link;
          headZ-&gt;link = NULL;
      }
  
      SortedMergeRecur(headX, headY, headZ);
  }
  
  if (headX != NULL &amp;&amp; headY != NULL)
  {
      if (headX-&gt;data &gt; headY-&gt;data)
      {
          if (headZ == NULL)
          {
              headZ = headY;
              headY = headY-&gt;link;
              headZ-&gt;link = NULL;
          }
          else
          {
              headZ-&gt;link = headY;
              headY = headY-&gt;link;
              headZ = headZ-&gt;link;
              headZ-&gt;link = NULL;
          }
      }
      else
      {
          if (headZ == NULL)
          {
              headZ = headX;
              headX = headX-&gt;link;
              headZ-&gt;link = NULL;
          }
          else
          {
              headZ-&gt;link = headX;
              headX = headX-&gt;link;
              headZ = headZ-&gt;link;
              headZ-&gt;link = NULL;
          }
      }
      SortedMergeRecur(headX, headY, headZ);
  }
  }
  </code></pre>
  
  <p>I know that parts of this could be simplified (like appending the whole list if either headX or headY are null) but I wrote it this way because I thought it might fix the problem, which it didn't....</p>