title: Deleting front node from a Singly Linked List in C
body: <p>I am trying to delete from a Singly Linked List, however, when I try to delete from the first element, it prints garbage. I think the problem comes from the delete_node function, however, I tried everything and I cannot figure it out. </p>
  
  <pre><code>    #include &lt;stdio.h&gt;//prinf
  #include &lt;stdlib.h&gt;//alloc mallco callo
  typedef struct node node;
   struct node{
      int number;
      node *next;
  };
  node *new_node(int num){
      node *n= (node*) malloc(sizeof(node));
      n-&gt;number=num;
      n-&gt;next=NULL;
      return n;
  }
  
  void node_free_all(node *n){
      if(n != NULL){
          node_free_all(n-&gt;next);
          free(n);
      }
  }
  void print_nodes(node *n){
      if(n != NULL){
          print_nodes(n-&gt;next);
          printf("Number is: %d\n",n-&gt;number);
      }
  }
  void delete_node(node *n, int num){
  
      node *rmNode= (node*)malloc(sizeof(node));
  
      //delete first
      if( n!= NULL &amp;&amp; n-&gt;number==num){
          rmNode = n;
          n=n-&gt;next;
          free(rmNode);
      }
      //all but first
      while(n != NULL){
          if(n-&gt;next != NULL &amp;&amp; n-&gt;next-&gt;number == num){
              rmNode= n-&gt;next;
              n-&gt;next = rmNode-&gt;next;
              free(rmNode);
              break;
          }
          n=n-&gt;next;
      }   
  
  }
  int main(){
      int i;
      node *head= (node*) malloc(sizeof(node));
      node *curr;
      head=NULL;
  
     for(i=1;i&lt;=10;i++) {
        curr = new_node(i);
        curr-&gt;next =head;
        head=curr;
      }
  
  
  
      printf("Everything:\n");
      print_nodes(head);
      printf("Deleting 1:\n");
      delete_node(head,1);
      print_nodes(head);
      printf("Deleting 5:\n");
      delete_node(head,5);
      print_nodes(head);
      printf("Deleting 2:\n");
      delete_node(head,2);
      print_nodes(head);
      printf("Deleting 3:\n");
      delete_node(head,3);
      print_nodes(head);
      printf("Deleting 10:\n");
      delete_node(head,10);
      print_nodes(head);
      printf("Deleting 9:\n");
      delete_node(head,9);
      print_nodes(head);
  
  node_free_all(head);
  // node_free_all(list);
  return 0;
  }
  </code></pre>
  
  <p>What am I doing wrong?</p>