179: sum of all elements in a linked list

PHOTO EMBED

Tue May 23 2023 15:21:58 GMT+0000 (Coordinated Universal Time)

Saved by @saakshi #c++

//using iteration
int Add(struct Node *p){
  int sum=0;
  while(p){
    sum= sum+ p->data;
    p= p->next;
  }
  return(sum);
}

//using recursion
int Add(struct node *p){
  if (p==0)
    return(0);
  else
    return Add(p->next) + p->data;
}

//the data will be added at the time of returning
content_copyCOPY