title: c++ Append to linked list
body: <p>I'm writing a piece of code to append a node to the end of a singly linked list, but it seems that it doesn't append anything at all. Can anybody give me some idea of what I'm doing wrong?</p>
  
  <pre><code>#include&lt;iostream&gt;
  using namespace std;
  
  struct Node{
          int val;
          Node* next;
          Node(int v) : val(v), next(NULL) {}
  };
  
  void append(Node &amp;head, int d){
          Node n = head;    
          while(n.next != NULL){
                  n = *n.next;
          }
          Node end(d);
          n.next = &amp;end;
  }
  
  int main(){
          Node head(0);
          for(int i=1;i&lt;5;i++){
                  append(head, i);
          }    
          Node n = head;
          while(n.next != NULL){ //print the linked list, result is 0
                  cout &lt;&lt; n.val&lt;&lt;" ";
                  n = *n.next;
          }
          cout&lt;&lt;n.val&lt;&lt;endl;
          return 0;
  }
  </code></pre>
  
  <p>EDIT: I changed the append() method to append a dynamically-allocated node each time, but it still doesn't work.</p>
  
  <pre><code>void append(Node &amp;head, int d){
              Node n = head;    
              while(n.next != NULL){
                      n = *n.next;
              }
              Node* end = new Node(d);
              n.next = end;
      }
  </code></pre>