Tuesday, November 1, 2011

Reversing linked list

Devise an algorithm to print elements of linked list in reverse without reversing the linked list. State the time and space complexities.

Solution :
Use recursion
Complexity :
  • Time : O(N)
  • Space : O(N)
Code :
void display_reverse(node *list){
       if(list){
              display_reverse(list->next);
              printf("%d\n", list->info);
       }
}

No comments:

Post a Comment