176. Recursive display for linked list

PHOTO EMBED

Tue May 23 2023 14:56:52 GMT+0000 (Coordinated Universal Time)

Saved by @saakshi #c++

//print in the given order as first the value is printed and then a call is made to the next node

void Display(struct Node *p){
  if (p!= NULL){
    printf("%d", p->data);  //first print
    Display(p->next);       //call next
  }
}

//print in reverse order as first the call is made and then the values are printed

void Display(struct Node *p){
  if (p!= NULL){
    Display(p->next);          //call next
    printf("%d", p->data);     //then print
  }
}
content_copyCOPY

time complexity : O(n) Space complexity : stack size : O(n)