Hacktoberfest

Home > Data Structures and Algorithms Questions > How to Reverse a Linked List ?

How to Reverse a Linked List ?



Answer:

Here's a code snippet to check if two binary trees are same or not.

static void reverse(struct Node** head_ref)
{
   struct Node* prev = NULL;
   struct Node* current = *head_ref;
   struct Node* next = NULL;
   while (current != NULL) {

     // Store next
     next = current->next;

     // Reverse current node's pointer
     current->next = prev;

     // Move pointers one position ahead.
     prev = current;
     current = next;
   }
   *head_ref = prev;
}