Given a singly linked list. The task is to find the average of all nodes of the given singly linked list.
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
- Initialise a pointer ptr with the head of the linked list and a sum variable with 0.
- Start traversing the linked list using a loop until all the nodes get traversed.
- Add the value of current node to the sum i.e. sum += ptr -> data .
- Increment the pointer to the next node of linked list i.e. ptr = ptr ->next .
- Divide sum by total number of node and Return the average.
Below is the implementation of the above approach:
// C# implementation to find the average
// of nodes of the Linked List
/* 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,
/* 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;
// Function to iteratively find the avg of
// nodes of the given linked list
static double avgOfNodes(Node head)
// if head = null
if (head == null)
int count = 0; // Initialize count
int sum = 0;
double avg = 0.0;
Node current = head; // Initialize current
while (current != null)
sum += current.data;
current = current.next;
// calculate average
avg = (double)sum / count;
// Driver Code
public static void Main(String args)
Node head = null;
// create linked list 188.8.131.52.1
Console.WriteLine(“Average of nodes = ” +
// This code is contributed by Rajput-Ji
Average of nodes = 5.2
Time complexity : O(n)
Where n is equal to number of nodes.
- Find sum of even and odd nodes in a linked list
- Find the sum of last n nodes of the given Linked List
- Find the product of first k nodes of the given Linked List
- Find the product of last N nodes of the given Linked List
- Find the common nodes in two singly linked list
- Splitting starting N nodes into new Circular Linked List while preserving the old nodes
- Append odd position nodes in reverse at the end of even positioned nodes in a Linked List
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
- Delete N nodes after M nodes of a linked list
- Program to find size of Doubly Linked List
- Python program to find middle of a linked list using one traversal
- Linked List Sum of Nodes Between 0s
- Sum of the nodes of a Circular Linked List
- Segregate even and odd nodes in a Linked List
- Sum and Product of all the nodes which are less than K in the linked list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.