Spread the post

Sum of N middle nodes of linked list

Sum of N middle nodes of linked list. Suppose linked list contain following nodes[1, 2, 3, 4, 5, 6, 7, 8, 9, 10].

Accepted Output

If there are two set of a middle nodes then choose first one.


[1,2,3,4,5,6,7,8,9] and N=4.

Two sets

A=[3, 4, 5]

B=[4, 5, 6]


Try it Yourself

View pointers and nodes.

Stack Areamainmiddle_node (int) ? root(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) struct Nodedata (int)= 10next (pointer)= NULL

Try it Yourself

Example: sum of 6 middle node is.

root_ptr10987654321Middle [6] Nodes [3+4+5+6+7+8] = 33

C program to Sum of N middle nodes of linked list. Iterative approach.


Spread the post

Recommended Posts: