Question #8307

an algorithm to find the average of the numbers in a linked list of numbers

Expert's answer

To find the average of numbers in a linked list you should calculate the total number of this numbers (N), calculate the sum of all this numbers (Sum). So the

expected value is Sum / N.

list l;

int sum = 0;

int n = 0;

while (l.next != 0) {

sum += l.number;

n++;

l = l.next;

}

return (float) sum / n;

## Comments

## Leave a comment