Tuesday, July 12, 2011

Simple C Program to reverse Singly LinkedList using recursion

Code:

void reverse(struct node** headRef) 
{
     struct node* first;
     struct node* rest;
     if (*headRef == NULL) 

            return;
     first = *headRef;
     rest = first->next;
     if (rest == NULL) return;
     reverse(&rest);
     first->next->next = first;
     first->next = NULL;
    *headRef = rest;
  }