title: How to Delete the last element in a linked list in C?
body: <p>I don't know how to delete the first and last element in a linked list in c. Below is my program, that uses a linked list. I have no idea how to actually delete the last element but I am able to find it. the element consists of an integer and the next pointer. if someone could please help me, it would be much appreciated.  </p>
  
  <pre><code>struct ListNode{
      int value;
      struct ListNode *next;
  };
  typedef struct ListNode Link;
  
  void deleteLast(Link *);
  void printList(Link *);
  void deleteFirst(Link *);
  
  int main(){
      Link *myList;
   Link *curr, *newlink;
   int i;
      curr = myList;
      for(i = 0; i &lt; 10; i++){
  
              newlink = (Link*) malloc(1*sizeof(Link));
              newlink-&gt;value = i*i;
              curr-&gt;next = newlink;
              curr = newlink;
      }
  
      curr-&gt;next = NULL;
      curr = myList-&gt;next;
      deleteFirst(curr);
      printList(curr);
      printf("\n");
  }
  void deleteLast(Link *head)
  {
      Link *curr;
      curr = head-&gt;next;
      while (curr-&gt;next!=NULL)
      {
          curr = curr-&gt;next;
      }
       free(curr-&gt;next);
  
      curr-&gt;next = NULL;
  
  }
  void printList(Link *head){
      Link *curr;
      curr = head-&gt;next;
  
      printf("[");
      if(curr!=NULL){
          printf("%d",curr-&gt;value);
          curr = curr-&gt;next;
      }
  
      while(curr != NULL){
          printf(", %d", curr-&gt;value);
          curr = curr-&gt;next;
      }
      printf("]\n");
  
  }
  void deleteFirst(Link *head){
      Link *curr;
      curr = head-&gt;next;
      free(curr-&gt;value);
      free(curr-&gt;next);
      printf("%d\t",curr-&gt;value);
  }
  </code></pre>
  
  <p>Nothing I try works, please can you help me. </p>