title: copying a LinkedList with a double pointer
body: <p>Inspired from this <a href="https://grisha.org/blog/2013/04/02/linus-on-understanding-pointers/" rel="nofollow noreferrer">article</a>, I've attempted to copy a LinkedList with double pointers (pointer to pointer).</p>
  
  <p>definition</p>
  
  <pre><code>typedef struct MyNode MyNode;
  
  struct MyNode {
      int value;
      MyNode *next;
  }
  </code></pre>
  
  <p>implementation</p>
  
  <pre><code>MyNode *CreateNode(int value) {
      MyNode *node = malloc(sizeof(MyNode));
      node-&gt;value = value;
      node-&gt;next = NULL;
      return node;
  }
  
  
  int main() {
      MyNode *an_existing_list = CreateNode(1); // edit, source list
      an_existing_list-&gt;next = CreateNode(2);
      an_existing_list-&gt;next-&gt;next = CreateNode(3);
  
  
      MyNode *cursor = an_existing_list; // an existing list as a source
  
  
      MyNode **previous = NULL; // double pointer reference
  
      while (cursor) { // iterate the exisitng list
  
          MyNode *temp = CreateNode(cursor-&gt;value); // create a new node  
  
          if (previous) { // if a node was created in the previous iteration
              (*previous)-&gt;next = temp; // access the actual node and set previous' next to this new node
          }
  
          previous = &amp;temp; // for the next iteration
  
          cursor = cursor-&gt;next; // next node
      }
      return 0;
  }
  </code></pre>
  
  <p><strong>Problem</strong>: I'm running into some of endless copy of the same node into next->next->next</p>