Spread the post

Sum of elements in given linked list

Suppose given linked list contain following nodes.

Input: 1 2 3 4 5 6 7 8 9

Output: 45


Initial result integer are zero. add temp pointer data value and result. and move temp pointer to next memory block. until this process temp pointer are not NULL. given function in below.

Time complexity O(n).

Given example:

Sum of given linked list

View insertion process.

Try it yourself

Code execution:

Stack Areamainresult (int) ? root(pointer)sum_of_elementresult (int) =10temp(pointer) Heap Areastruct Nodedata (int)= 1next (pointer) struct Nodedata (int)= 2next (pointer) struct Nodedata (int)= 3next (pointer) struct Nodedata (int)= 4next (pointer) struct Nodedata (int)= 5next (pointer) struct Nodedata (int)= 6next (pointer) struct Nodedata (int)= 7next (pointer) struct Nodedata (int)= 8next (pointer) struct Nodedata (int)= 9next (pointer)= NULL

Note that not given all step of execution process here.View How to insert linked list element and so on.

Try it yourself

C program to sum of all given elemenet of linked list.


Spread the post

Recommended Posts: