title: remove a node in linked list using pointer to pointer in C
body: <p>I want to remove nodes whose values are N in a linked list. Below is the definition of the linked list and remove function I wrote.</p>
  
  <pre><code>typedef struct LinkedList {
      int val;
      struct LinkedList *next;
  } LinkedList;
  
  void removeNode(int val, LinkedList **head) {
     LinkedList *prev = *head;
  
     while (prev &amp;&amp; prev-&gt;val == val)
         prev = prev-&gt;next;
     head = &amp;prev;
     if (*head == NULL)
         return;
  
     // other stuff
  }
  </code></pre>
  
  <p>In order to test if the pointer to pointer works or not in this function, I wrote the following function.</p>
  
  <pre><code>int main() {
      LinkedList root;
      root.val = 1;
      root.next = NULL;
      LinkedList *head = &amp;root;
  
      removeNode(1, &amp;head);
  
      if (head == NULL)
          printf("%s\d", "Empty");
      else
          printf("%d\n", head-&gt;val);
  
      return 0;
  }
  </code></pre>
  
  <p>I think the output will be "Empty" as <code>removeNode</code> function will modify the <code>head</code> pointer to <code>NULL</code>. However, the output is <code>1</code>. </p>
  
  <p>My question is why the pointer to pointer doesn't change <code>head</code> in <code>removeNode</code> function. All suggestions are welcome.</p>
  
  <p>Update:</p>
  
  <p>Thanks for Michael's reply. The correct way to modify <code>head</code> is <code>*head = prev</code>, not 
  <code>head = &amp;prev</code>.</p>