Lily L.

asked • 06/06/23

Recursion in C - Explain Given Solution

Question: Write a recursive function to add to each number in a linked list the original values of its two neighbors. For example, 10->2->3->5->NULL becomes 12->15->10->8->NULL. Each node in the list has an integer number (int number) and a pointer to the next node (NODE

*next). Assume the list is not empty.


The answer given is below. I don't understand how to get this answer. Could someone explain?


int add (NODE *p, int a) {
if (p == NULL)
return 0;
int n, r;
r = add(p->next, p->number);
n = p->number;
p->number += a+r;
return n;

1 Expert Answer

By:

Derek B. answered • 06/06/23

Tutor
5 (5)

BS in Computer Information Technology, Specializing in Cloud/Web Dev.

Lily L.

Thanks so much, this helped a lot!
Report

06/09/23

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.