Related Articles
Program to find average of all nodes in a Linked List
• Last Updated : 13 Feb, 2020

Given a singly linked list. The task is to find the average of all nodes of the given singly linked list.

Examples:

```Input: 7->6->8->4->1
Output: 26
Average of nodes:
(7 + 6 + 8 + 4 + 1 ) / 5 = 5.2

Input: 1->7->3->9->11->5
Output: 6
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Iterative Solution:

1. Initialise a pointer ptr with the head of the linked list and a sum variable with 0.
2. Start traversing the linked list using a loop until all the nodes get traversed.
3. Add the value of current node to the sum i.e. sum += ptr -> data .
4. Increment the pointer to the next node of linked list i.e. ptr = ptr ->next .
5. Divide sum by total number of node and Return the average.

Below is the implementation of the above approach:

## C++

 `// C++ implementation to find the average of ` `// nodes of the Linked List ` ` `  `#include ` `using` `namespace` `std; ` ` `  `/* A Linked list node */` `struct` `Node { ` `    ``int` `data; ` `    ``struct` `Node* next; ` `}; ` ` `  `// function to insert a node at the ` `// beginning of the linked list ` `void` `push(``struct` `Node** head_ref, ``int` `new_data) ` `{ ` `    ``/* allocate node */` `    ``struct` `Node* new_node = ``new` `Node; ` ` `  `    ``/* put in the data */` `    ``new_node->data = new_data; ` ` `  `    ``/* link the old list to the new node */` `    ``new_node->next = (*head_ref); ` ` `  `    ``/* move the head to point to the new node */` `    ``(*head_ref) = new_node; ` `} ` ` `  `// Function to iteratively find the avg of ` `// nodes of the given linked list ` `float` `avgOfNodes(``struct` `Node* head) ` `{ ` `    ``// if head = NULL ` `    ``if` `(!head) ` `        ``return` `-1; ` ` `  `    ``int` `count = 0; ``// Initialize count ` `    ``int` `sum = 0; ` `    ``float` `avg = 0.0; ` ` `  `    ``struct` `Node* current = head; ``// Initialize current ` `    ``while` `(current != NULL) { ` `        ``count++; ` `        ``sum += current->data; ` `        ``current = current->next; ` `    ``} ` ` `  `    ``// calculate average ` `    ``avg = (``double``)sum / count; ` ` `  `    ``return` `avg; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``struct` `Node* head = NULL; ` ` `  `    ``// create linked list 7->6->8->4->1 ` `    ``push(&head, 7); ` `    ``push(&head, 6); ` `    ``push(&head, 8); ` `    ``push(&head, 4); ` `    ``push(&head, 1); ` ` `  `    ``cout << ``"Average of nodes = "` `<< avgOfNodes(head); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation to find the average of ` `// nodes of the Linked List ` `class` `GFG ` `{  ` `     `  `/* A Linked list node */` `static` `class` `Node  ` `{ ` `    ``int` `data; ` `    ``Node next; ` `}; ` ` `  `// function to insert a node at the ` `// beginning of the linked list ` `static` `Node push(Node head_ref, ``int` `new_data) ` `{ ` `    ``/* allocate node */` `    ``Node new_node = ``new` `Node(); ` ` `  `    ``/* put in the data */` `    ``new_node.data = new_data; ` ` `  `    ``/* link the old list to the new node */` `    ``new_node.next = (head_ref); ` ` `  `    ``/* move the head to point to the new node */` `    ``(head_ref) = new_node; ` `    ``return` `head_ref; ` `} ` ` `  `// Function to iteratively find the avg of ` `// nodes of the given linked list ` `static` `double` `avgOfNodes(Node head) ` `{ ` `    ``// if head = null ` `    ``if` `(head == ``null``) ` `        ``return` `-``1``; ` ` `  `    ``int` `count = ``0``; ``// Initialize count ` `    ``int` `sum = ``0``; ` `    ``double` `avg = ``0.0``; ` ` `  `    ``Node current = head; ``// Initialize current ` `    ``while` `(current != ``null``) ` `    ``{ ` `        ``count++; ` `        ``sum += current.data; ` `        ``current = current.next; ` `    ``} ` ` `  `    ``// calculate average ` `    ``avg = (``double``)sum / count; ` ` `  `    ``return` `avg; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main(String args[]) ` `{ ` `    ``Node head = ``null``; ` ` `  `    ``// create linked list 7.6.8.4.1 ` `    ``head=push(head, ``7``); ` `    ``head=push(head, ``6``); ` `    ``head=push(head, ``8``); ` `    ``head=push(head, ``4``); ` `    ``head=push(head, ``1``); ` ` `  `    ``System.out.println(``"Average of nodes = "` `+ avgOfNodes(head)); ` `} ` `} ` `// This code is contributed by Arnab Kundu `

## Python3

 `# Python3 implementation to find the average of  ` `# nodes of the Linked List  ` `class` `newNode:  ` ` `  `    ``# Constructor to create a new node  ` `    ``def` `__init__(``self``, data):  ` `        ``self``.data ``=` `data  ` `        ``self``.``next` `=` `None` ` `  `# function to insert a node at the  ` `# beginning of the linked list  ` `def` `push(node,data): ` `     `  `    ``''' allocate node '''` `    ``if` `(node ``=``=` `None``):  ` `        ``return` `(newNode(data))  ` `     `  `    ``else``: ` `        ``node.``next` `=` `push(node.``next``, data)  ` `        ``return` `node  ` ` `  `# Function to iteratively find the avg of  ` `# nodes of the given linked list  ` `def` `avgOfNodes(head): ` `     `  `    ``# if head = NULL  ` `    ``if` `(head ``=``=` `None``): ` `        ``return` `-``1` `     `  `    ``count ``=` `0` `# Initialize count  ` `    ``sum` `=` `0` `    ``avg ``=` `0.0` ` `  `    ``while` `(head !``=` `None``): ` `        ``count ``+``=` `1` `        ``sum` `+``=` `head.data  ` `        ``head ``=` `head.``next` `     `  `    ``# calculate average  ` `    ``avg ``=` `sum` `/` `count  ` `    ``return` `avg  ` ` `  `# Driver Code  ` ` `  `# create linked list 7.6.8.4.1  ` `head ``=` `newNode(``7``)  ` `push(head, ``6``)  ` `push(head, ``8``)  ` `push(head, ``4``)  ` `push(head, ``1``)  ` `print``(``"Average of nodes = "``,avgOfNodes(head)) ` ` `  `# This code is contributed by ` `# Shubham Singh(SHUBHAMSINGH10)  `

## C#

 `// C# implementation to find the average  ` `// of nodes of the Linked List  ` `using` `System; ` ` `  `class` `GFG  ` `{  ` `     `  `/* A Linked list node */` `public` `class` `Node  ` `{  ` `    ``public` `int` `data;  ` `    ``public` `Node next;  ` `};  ` ` `  `// function to insert a node at the  ` `// beginning of the linked list  ` `static` `Node push(Node head_ref,  ` `                 ``int` `new_data)  ` `{  ` `    ``/* allocate node */` `    ``Node new_node = ``new` `Node();  ` ` `  `    ``/* put in the data */` `    ``new_node.data = new_data;  ` ` `  `    ``/* link the old list to the new node */` `    ``new_node.next = (head_ref);  ` ` `  `    ``/* move the head to point to the new node */` `    ``(head_ref) = new_node;  ` `    ``return` `head_ref;  ` `}  ` ` `  `// Function to iteratively find the avg of  ` `// nodes of the given linked list  ` `static` `double` `avgOfNodes(Node head)  ` `{  ` `    ``// if head = null  ` `    ``if` `(head == ``null``)  ` `        ``return` `-1;  ` ` `  `    ``int` `count = 0; ``// Initialize count  ` `    ``int` `sum = 0;  ` `    ``double` `avg = 0.0;  ` ` `  `    ``Node current = head; ``// Initialize current  ` `    ``while` `(current != ``null``)  ` `    ``{  ` `        ``count++;  ` `        ``sum += current.data;  ` `        ``current = current.next;  ` `    ``}  ` ` `  `    ``// calculate average  ` `    ``avg = (``double``)sum / count;  ` ` `  `    ``return` `avg;  ` `}  ` ` `  `// Driver Code  ` `public` `static` `void` `Main(String []args)  ` `{  ` `    ``Node head = ``null``;  ` ` `  `    ``// create linked list 7.6.8.4.1  ` `    ``head=push(head, 7);  ` `    ``head=push(head, 6);  ` `    ``head=push(head, 8);  ` `    ``head=push(head, 4);  ` `    ``head=push(head, 1);  ` ` `  `    ``Console.WriteLine(``"Average of nodes = "` `+  ` `                           ``avgOfNodes(head));  ` `}  ` `}  ` ` `  `// This code is contributed by Rajput-Ji `

Output:

```Average of nodes = 5.2
```

Time complexity : O(n)
Where n is equal to number of nodes.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :