Wednesday, July 26, 2017

reverse

void LLreverse(LL_t* list)
{
  //creates a new head
  LL_t* l = malloc (sizeof(LL_t));
  if(l){
    l->head = list->head;
  }
  if(l && list->head){
    list->head = list->head->next; //moves the list head to the next element
    l->head->next = NULL; //this is the last element of the new list

    //reverses the list
    node_t* el = list->head;
    while(el){
      list->head = list->head->next;
      el->next = l->head;
      l->head = el;
      el = list->head;
    }
    // makes the list->head point to new list
    list->head = l->head;
    //destroys the list
    l->head = NULL;
    free(l);
  }
}

No comments:

Post a Comment