# Sum of smaller elements of nodes in a linked list

Suppose that linked list combination of two data element which are contain in integer value. Sum the all smallest nodes value of this linked list.

We can solve this problem by iterator linked list node from head to last node and compare node value. Before write an algorithm to solve this problem consider following test cases

1) There is possible to linked list empty. In this situation display a message(linked list is empty).

2) There is possible to node are contain both same value. then add any one value of the resultant.

Suppose we are inserted the following ((3 9),(11 4),(3 3),(6 1),(2 9),(4 7) ) node in a sequence.

Here given code implementation process.

``````//C Program
//Sum of smallest element of linked list
#include<stdio.h>
#include <stdlib.h> //for malloc function

//create structure
struct Node{
int data1,data2;
struct Node*next;
};
//function prototype
void insert(struct Node**,int,int);
void display(struct Node*);

//insert Node element
void insert(struct Node**head,int v1,int v2){
//Create dynamic node
struct Node*node=(struct Node*)malloc(sizeof(struct Node));
if(node==NULL){
printf("Memory overflow\n");
}else{
//set data values
node->data1=v1;
node->data2=v2;

node->next=NULL;
}else{
//find last node
while(temp->next!=NULL){
temp=temp->next;
}
//add node at last possition
temp->next=node;
}
}
}
//Display element of Node
void display(struct Node*temp){

if(temp==NULL){
}
while(temp!=NULL){
printf("(%d %d)->",temp->data1,temp->data2);
temp=temp->next;
}
printf("NULL");
}
//add all smallest element
void min_sum(struct Node*temp){
if(temp==NULL){
}
int result=0;
while(temp!=NULL){

if(temp->data1<temp->data2){
//when node data1 is smaller
result+=temp->data1;
}else{
//when  data2 is  less than and equal to  data1
result+=temp->data2;
}
temp=temp->next;
}
printf("\nSum of Smaller Elements: %d\n",result );
}
int main(){
//Insert element of linked list

//Display all node
return 0;
}``````

#### Output

``````(3 9)->(11 4)->(3 3)->(6 1)->(2 9)->(4 7)->NULL
Sum of Smaller Elements: 17``````
``````//C++ Program
//Sum of smallest element of linked list
#include<iostream>
using namespace std;

//create structure
struct Node{
int data1,data2;
struct Node*next;
};
public:
void insert(int,int);
void display();
void min_sum();
};
}
//insert node at end of linked list
void LinkedList::insert(int v1,int v2){
//Create dynamic node
struct Node*node=new Node;
if(node==NULL){
cout<<"Memory overflow\n";
}else{
node->data1=v1;
node->data2=v2;
node->next=NULL;
//base condition
}else{
while(temp->next!=NULL){
temp=temp->next;
}
//add newly node at last
temp->next=node;
}
}
}
//display all node value in linked list
}
else{
cout<<"Linked List : ";
while(temp!=NULL){
//print node value
cout<<"("<<temp->data1<<","<<temp->data2<<")->";
temp=temp->next;
}
cout<<"NULL";
}

}

//add all smallest element
if(temp==NULL){
}
int result=0;
while(temp!=NULL){

if(temp->data1<temp->data2){
//when node data1 is smaller
result+=temp->data1;
}else{
//when  data2 is  less than and equal to  data1
result+=temp->data2;
}
temp=temp->next;
}
cout<<"\nSum of Smaller Elements: "<<result <<endl;
}
int main(){

//create object
//insert element of linked list

obj.insert(3,9);
obj.insert(11,4);
obj.insert(3,3);
obj.insert(6,1);
obj.insert(2,9);
obj.insert(4,7);

//display all node
obj.display();
obj.min_sum();
return 0;
}
``````

#### Output

``````Linked List : (3,9)->(11,4)->(3,3)->(6,1)->(2,9)->(4,7)->NULL
Sum of Smaller Elements: 17
``````
``````//Java Program
//Sum of smallest element of linked list

public class Node{
int data1,data2;
Node next;
}
//Class constructors
}
//insert element
public void insert(int v1,int v2){
//Create  node
Node node=new Node();
node.data1=v1;
node.data2=v2;
node.next=null;
else{
//find last node
while(temp.next!=null){
temp=temp.next;
}
temp.next=node;
}

}
public void minSum(){
if(temp==null){
}
int result=0;
while(temp!=null){

if(temp.data1<temp.data2){
//when node data1 is smaller
result+=temp.data1;
}else{
//when  data2 is  less than and equal to  data1
result+=temp.data2;
}
temp=temp.next;
}
System.out.println("Sum of Smaller Elements: "+result);

}

//Display all Linked List elements
public void display(){
System.out.print("Linked List Element :");
while(temp!=null){
System.out.print("("+temp.data1+","+ temp.data2 +").");
temp=temp.next;
}
}else{
}
}

static public void main(String[] args) {

//insert element
obj.insert(3,9);
obj.insert(11,4);
obj.insert(3,3);
obj.insert(6,1);
obj.insert(2,9);
obj.insert(4,7);

//display all node
obj.display();
obj.minSum();
}
}``````

#### Output

``````Linked List : (3,9)->(11,4)->(3,3)->(6,1)->(2,9)->(4,7)->NULL
Sum of Smaller Elements: 17``````
``````#Python Program
#Sum of smallest element of linked list
class Node:
def __init__(self,data1,data2):
self.data1=data1
self.data2=data2
self.next=None

def __init__(self):
#Assign default value

#insert new node to linked list
def insert(self,v1,v2):
node=Node(v1,v2)
node.next=None
else:
while temp.next!=None:
temp=temp.next
temp.next=node

def display(self):
return

print("Linked List Elements : ")
while(temp!=None):
print("(",temp.data1,",",temp.data2,")->",end="")
temp=temp.next
print("None")

#Sum of all smallest element
def minSum(self):
if(temp==None):

result=0
while(temp!=None):

if(temp.data1<temp.data2):
#when node data1 is smaller
result+=temp.data1
else:
#when  data2 is  less than and equal to  data1
result+=temp.data2

temp=temp.next

print("Sum of Smaller Elements: ",result)

def main():
#Create Object of class Linked
obj.insert(3,9)
obj.insert(11,4)
obj.insert(3,3)
obj.insert(6,1)
obj.insert(2,9)
obj.insert(4,7)

#display all node
obj.display()
obj.minSum()
if __name__=="__main__":
main()``````

#### Output

``````Linked List Elements :
( 3 , 9 )->( 11 , 4 )->( 3 , 3 )->( 6 , 1 )->( 2 , 9 )->( 4 , 7 )->None
Sum of Smaller Elements:  17
``````
``````//C# Program
//Sum of smallest element of linked list
using System;
//node class
public class Node{
public  int data1,data2;
public  Node next;
}
class Program
{
public Program(){
}
//insert node of linked list
public void insert(int v1,int v2){
Node newNode=new Node();
newNode.data1=v1;
newNode.data2=v2;
newNode.next=null;
else{
//get last node
while(temp.next!=null){
temp=temp.next;
}
temp.next=newNode;
}
}
//display linked list nodes value
public void display(){
Console.Write("Empty List");
}else{
while(temp!=null){
Console.Write("({0},{1})->",temp.data1,temp.data2);
temp=temp.next;
}
Console.Write("null");
}
}
public void minSum(){
if(temp==null){
}
int result=0;
while(temp!=null){

if(temp.data1<temp.data2){
//when node data1 is smaller
result+=temp.data1;
}else{
//when  data2 is  less than and equal to  data1
result+=temp.data2;
}
temp=temp.next;
}
Console.Write("\nSum of Smaller Elements: {0}",result);

}
static void Main()
{
Program obj=new Program();
//insert element
obj.insert(3,9);
obj.insert(11,4);
obj.insert(3,3);
obj.insert(6,1);
obj.insert(2,9);
obj.insert(4,7);

//display all node
obj.display();
obj.minSum();
}
}``````

#### Output

``````(3,9)->(11,4)->(3,3)->(6,1)->(2,9)->(4,7)->null
Sum of Smaller Elements: 17
``````

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.