In this blog, we will learn how to do the sum of linked list in java. This is a basic program for linked list

Code for Sum of Linked List in Java

class Node<T>{

T data;

Node<T> next;

Node(T data){

this.data =data;

}

}

public class Testing{

// function to do the sum of my linked list

public static int sum(Node<Integer> head) {

int sum =0;

while (head!=null) {

sum += head.data;

head = head.next;

}

return sum;

}

public static void main(String[] args) {

//create my linked list

Node<Integer> a = new Node<>(1);

Node<Integer> b = new Node<>(2);

Node<Integer> c = new Node<>(2);

Node<Integer> d = new Node<>(3);

// create the relations

a.next=b;

b.next =c;

c.next=d;

System.out.println(sum(a));

}

}

Previous Blog : Length of the Linked List

Next Blog :

## 0 Comments

if you are not getting it then ask i am glad to help