Sum of Linked List in Java


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){ =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 =;
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; =c;;


Next Blog :

Post a Comment