# Sum of all distinct nodes in a linked list

Given a linked list, Find the sum of all distinct nodes of this linked list. That is simple problem but comparing one node to other and finding unique nodes this process are tack O(n^2) time.

Suppose we are inserted the following (4, 5, 2, 3, 5, 7) node in a sequence.

Here given code implementation process.

``````//C Program
//Sum of all distinct nodes in linked list
#include<stdio.h>
#include <stdlib.h> //for malloc function

//create structure
struct Node{
int data;
struct Node*next;
};
//function prototype
void insert(struct Node**,int);
void display(struct Node*);
//insert Node element
void insert(struct Node**head,int value){
//Create dynamic node
struct Node*node=(struct Node*)malloc(sizeof(struct Node));
if(node==NULL){
printf("Memory overflow\n");
}else{
node->data=value;
node->next=NULL;
}else{
//find last node
while(temp->next!=NULL){
temp=temp->next;
}
//add node at last possition
temp->next=node;
}
}
}
//Return the sum of distinct element

int result=0,flag=0;
while(temp!=NULL)
{
//reset checker pointer
//flag are used to check exist element is repeated or not
flag=1;
while(checker!=NULL)
{
if(checker!=temp && checker->data==temp->data)
{
//when repeated element are found
flag=0;
break;
}
checker=checker->next;
}
if(flag==1)
{
//when unique element
result+=temp->data;
}
temp=temp->next;
}

return result;

}
//Display element of Node
void display(struct Node*temp){

if(temp==NULL){
}else{
printf("Linked List : ");
while(temp!=NULL){
printf("%d  ",temp->data);
temp=temp->next;
}
}

}
int main(){
//create node pointer
//insert element of linked list

//display all node
}``````

#### Output

``````Linked List : 4  5  2  3  5  7
Distinct Sum: 16``````
``````//C++ Program
//Sum of all distinct nodes in linked list
#include<iostream>
using namespace std;

//create structure
struct Node{
int data;
struct Node*next;
};
public:
void insert(int);
void display();
int distinct_sum();
};
}
//insert node at end of linked list
//Create dynamic node
struct Node*node=new Node;
if(node==NULL){
cout<<"Memory overflow\n";
}else{
node->data=value;
node->next=NULL;
//base condition
}else{
while(temp->next!=NULL){
temp=temp->next;
}
//add newly node at last
temp->next=node;
}
}
}
//Return the sum of distinct element

int result=0,flag=0;
while(temp!=NULL)
{
//reset checker pointer
//flag are used to check exist element is repeated or not
flag=1;
while(checker!=NULL)
{
if(checker!=temp && checker->data==temp->data)
{
//when repeated element are found
flag=0;
break;
}
checker=checker->next;
}
if(flag==1)
{
//when unique element
result+=temp->data;
}
temp=temp->next;
}

return result;

}
//display all node value in linked list
}
else{
cout<<"Linked List : ";
while(temp!=NULL){
//print node value
cout<<temp->data<<" ";
temp=temp->next;
}
}

}
int main(){

//create object
//insert element of linked list
obj.insert(4);
obj.insert(5);
obj.insert(2);
obj.insert(3);
obj.insert(5);
obj.insert(7);
//display all node
obj.display();
cout<<"\nDistinct Node sum : "<<obj.distinct_sum()<<endl;
}``````

#### Output

``````Linked List : 4 5 2 3 5 7
Distinct Node sum : 16
``````
``````//Java Program
//Sum of all distinct nodes in linked list
class Node {
public int data;
public Node next;
public Node(int value) {
data = value;
next = null;
}
}

public class LinkedList {

//Class constructors
}
//insert element
public void insert(int value) {
//Create  node
Node node = new Node(value);

if (head == null) head = node;
else {
Node temp = head;
//find last node
while (temp.next != null) {
temp = temp.next;
}
temp.next = node;
}

}
//Display all Linked List elements
public void display() {
if (head != null) {
System.out.print("Linked List Element :");
Node temp = head;
while (temp != null) {
System.out.print("  " + temp.data);
temp = temp.next;
}
} else {
}
}
//return the sum of distinct element
public int distinctSum() {

int result = 0, flag = 0;
Node temp = head, checker = null;
while (temp != null) {
//reset checker pointer
//flag are used to check exist element is repeated or not
flag = 1;
while (checker != null) {
if (checker != temp && checker.data == temp.data) {
//when repeated element are found
flag = 0;
break;
}
checker = checker.next;
}
if (flag == 1) {
//when unique element
result += temp.data;
}
temp = temp.next;
}

return result;

}
public static void main(String[] args) {

//insert linked list element
obj.insert(4);
obj.insert(5);
obj.insert(2);
obj.insert(3);
obj.insert(5);
obj.insert(7);
//display all node
obj.display();
System.out.print("\nDistinct Node sum : " + obj.distinctSum());

}
}``````

#### Output

``````Linked List Element :  4  5  2  3  5  7
Distinct Node sum : 16``````
``````#Python
#Sum of all distinct nodes in linked list
class Node:
def __init__(self,data):
self.data=data
self.next=None

def __init__(self):
#Assign default value

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

def display(self):
print("Linked List Elements : ")

while(temp!=None):
print(temp.data,end=" ")
temp=temp.next
print("\n")

def distinctSum(self):

result=0
flag=0
checker=None
while(temp!=None):

#reset checker pointer
#flag are used to check exist element is repeated or not
flag=1;
while(checker!=None):

if(checker!=temp and checker.data==temp.data):
#when repeated element are found
flag=0;
break;

checker=checker.next;

if(flag==1):
#when unique element
result+=temp.data;

temp=temp.next;

return result;

def main():
#Create Object of class Linked
#insert linked list element
obj.insert(4);
obj.insert(5);
obj.insert(2);
obj.insert(3);
obj.insert(5);
obj.insert(7);
#display all node
obj.display();
print("Distinct Node sum : ",obj.distinctSum());

if __name__=="__main__":
main()``````

#### Output

``````Linked List Elements :
4 5 2 3 5 7

Distinct Node sum :  16``````
``````//C# Program
//Sum of all distinct nodes in linked list
using System;
//node class
public class Node{
public  int data;
public  Node next;
}
class Program
{
public Program(){
}
//insert node of linked list
public void insert(int data){
Node newNode=new Node();
newNode.data=data;
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}",temp.data);
temp=temp.next;
}

}
}
//return the sum of distinct element
public int distinctSum(){

int result=0,flag=0;
while(temp!=null)
{
//reset checker pointer
//flag are used to check exist element is repeated or not
flag=1;
while(checker!=null)
{
if(checker!=temp && checker.data==temp.data)
{
//when repeated element are found
flag=0;
break;
}
checker=checker.next;
}
if(flag==1)
{
//when unique element
result+=temp.data;
}
temp=temp.next;
}

return result;

}
static void Main()
{
Program obj=new Program();
//insert linked list element
obj.insert(4);
obj.insert(5);
obj.insert(2);
obj.insert(3);
obj.insert(5);
obj.insert(7);
//display all node
obj.display();
Console.Write("\nDistinct Node sum : "+obj.distinctSum());

}
}``````

#### Output

``````4 5 2 3 5 7
Distinct Node sum : 16``````
``````<?php
//Php program
//Sum of all distinct nodes in linked list
class Node
{
public \$data;
public \$next;
function __construct(\$data)
{
\$this->data = \$data;
\$this->next = NULL;
}
}

function __construct()
{
}
/*
* Append the Given data value at end of linked list
* Fun : insert
* Parm: data value
*@return None
*/
function insert(\$data)
{
\$newNode=new Node(\$data);
{
}else{
//find last node of linked list
while(\$temp->next!=NULL){
\$temp=\$temp->next;
}
//add new node to last of linked list
\$temp->next=\$newNode;
}
}
//Display all inserted node in linked list
function display()
{
{
echo "Empty Linked List";
}
else{
echo "Linked List :";
while(\$temp!=NULL){
//display node value
echo "  ".\$temp->data;
\$temp=\$temp->next; //visit to next node
}
}
}
//Return the sum of distinct element
function distinct_sum(){

\$result=0;
\$flag=0;
\$checker=NULL;
while(\$temp!=NULL)
{
//reset checker pointer
//flag are used to check exist element is repeated or not
\$flag=1;
while(\$checker!=NULL)
{
if(\$checker!=\$temp && \$checker->data==\$temp->data)
{
//when repeated element are found
\$flag=0;
break;
}
\$checker=\$checker->next;
}
if(\$flag==1)
{
//when unique element
\$result+=\$temp->data;
}
\$temp=\$temp->next;
}

return \$result;

}
}
function main(){
//Make a object of LinkedList class

/*
*Insert following nodes in linked list
*/
\$obj->insert(4);
\$obj->insert(5);
\$obj->insert(2);
\$obj->insert(3);
\$obj->insert(5);
\$obj->insert(7);
\$obj->display();

echo "<br>Distinct Sum: ".\$obj->distinct_sum();
}
main();
?>``````

#### Output

``````Linked List : 4 5 2 3 5 7
Distinct Sum: 16``````

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.