title: Passing a pointer to a linked list in C++
body: <p>I have a fairly basic program that is intended to sort a list of numbers via a Linked List.</p>
  
  <p>Where I am getting hung up is when the element needs to be inserted at the beginning of the list.  Here is the chunk of code in question</p>
  
  <p>Assume that root->x = 15 and assume that the user inputs 12 when prompted:</p>
  
  <pre><code>void addNode(node *root)
  {
  int check = 0;  //To break the loop
  node *current = root;   //Starts at the head of the linked list
  node *temp = new node;
  
  
  cout &lt;&lt; "Enter a value for x" &lt;&lt; endl;
  cin &gt;&gt; temp-&gt;x;
  cin.ignore(100,'\n');
  
  
  if(temp-&gt;x &lt; root-&gt;x)
  {
      cout &lt;&lt; "first" &lt;&lt; endl;
      temp-&gt;next=root;
      root=temp;
  
          cout &lt;&lt; root-&gt;x &lt;&lt; " " &lt;&lt; root-&gt;next-&gt;x; //Displays 12 15, the correct response
  }
  </code></pre>
  
  <p>But if, after running this function, I try</p>
  
  <pre><code>cout &lt;&lt; root-&gt;x;
  </code></pre>
  
  <p>Back in main(), it displays 15 again.  So the code </p>
  
  <pre><code>root=temp;
  </code></pre>
  
  <p>is being lost once I leave the function.  Now other changes to *root, such as adding another element to the LL and pointing root->next to it, are being carried over.</p>
  
  <p>Suggestions?</p>