Reverse a Simple Linked List in C


Some more advanced functions for simple linked lists such as reserving could be a good reference. The following code snippet is a simple function for reversing a simple linked list.

Please check also

struct node_t * reverse(struct node_t * head)
   struct node_t * temp = head;
   struct node_t * new_head = NULL;
   struct node_t * next_node = NULL;

   if (head == NULL)
     return NULL;

   while (temp != NULL)
      next_node = temp->next;
      temp->next = new_head;
      new_head = temp;
      temp = next_node;

   return new_head;
Disclaimer: The present content may not be used for training artificial intelligence or machine learning algorithms. All other uses, including search, entertainment, and commercial use, are permitted.