Program to find average of all nodes in a Linked List
Last Updated :
19 Sep, 2022
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
Iterative Solution:
- 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++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
void push( struct Node** head_ref, int new_data)
{
struct Node* new_node = new Node;
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
float avgOfNodes( struct Node* head)
{
if (!head)
return -1;
int count = 0;
int sum = 0;
float avg = 0.0;
struct Node* current = head;
while (current != NULL) {
count++;
sum += current->data;
current = current->next;
}
avg = ( double )sum / count;
return avg;
}
int main()
{
struct Node* head = NULL;
push(&head, 7);
push(&head, 6);
push(&head, 8);
push(&head, 4);
push(&head, 1);
cout << "Average of nodes = " << avgOfNodes(head);
return 0;
}
|
Java
class GFG
{
static class Node
{
int data;
Node next;
};
static Node push(Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
return head_ref;
}
static double avgOfNodes(Node head)
{
if (head == null )
return - 1 ;
int count = 0 ;
int sum = 0 ;
double avg = 0.0 ;
Node current = head;
while (current != null )
{
count++;
sum += current.data;
current = current.next;
}
avg = ( double )sum / count;
return avg;
}
public static void main(String args[])
{
Node head = null ;
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));
}
}
|
Python3
class newNode:
def __init__( self , data):
self .data = data
self . next = None
def push(node,data):
if (node = = None ):
return (newNode(data))
else :
node. next = push(node. next , data)
return node
def avgOfNodes(head):
if (head = = None ):
return - 1
count = 0
sum = 0
avg = 0.0
while (head ! = None ):
count + = 1
sum + = head.data
head = head. next
avg = sum / count
return avg
head = newNode( 7 )
push(head, 6 )
push(head, 8 )
push(head, 4 )
push(head, 1 )
print ( "Average of nodes = " ,avgOfNodes(head))
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node next;
};
static Node push(Node head_ref,
int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
return head_ref;
}
static double avgOfNodes(Node head)
{
if (head == null )
return -1;
int count = 0;
int sum = 0;
double avg = 0.0;
Node current = head;
while (current != null )
{
count++;
sum += current.data;
current = current.next;
}
avg = ( double )sum / count;
return avg;
}
public static void Main(String []args)
{
Node head = null ;
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));
}
}
|
Javascript
<script>
class Node {
constructor(val) {
this .data = val;
this .next = null ;
}
}
function push(head_ref , new_data) {
var new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
return head_ref;
}
function avgOfNodes(head) {
if (head == null )
return -1;
var count = 0;
var sum = 0;
var avg = 0.0;
var current = head;
while (current != null ) {
count++;
sum += current.data;
current = current.next;
}
avg = sum / count;
return avg;
}
var head = null ;
head = push(head, 7);
head = push(head, 6);
head = push(head, 8);
head = push(head, 4);
head = push(head, 1);
document.write( "Average of nodes = " + avgOfNodes(head));
</script>
|
Output
Average of nodes = 5.2
Time complexity: O(n), Where n is equal to number of nodes
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...