Open In App

JavaScript Program to Find Sum of Elements in Linked List

Last Updated : 29 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

We are given a linked list, we have to find the sum of all the elements of that linked list. A linked list is a data structure to store data where each piece of information is connected to the next one, forming a chain. Each piece has its data and knows where to find the next piece. It’s a basic but useful method to organize and manage data.

There are several approaches to finding the sum of the elements in a linked list using JavaScript which are as follows:

Using While Loop

To find the sum of a linked list, we traverse each number in the list, adding them together. The function utilizes a while loop to iterate through each number in the linked list and find its sum. Starting with zero, the function adds the numbers one by one until it reaches the end of the list, providing the total sum of all numbers in the linked list.

Example: JavaScript program defining a linked list and populates it with values, and calculates the sum of its elements.

Javascript




class Node {
    constructor(data) {
        this.data = data;
        this.next = null;
    }
}
 
class LinkedList {
    constructor() {
        this.head = null;
    }
 
    add(data) {
        let new_node = new Node(data);
        new_node.next = this.head;
        this.head = new_node;
    }
}
 
function sum(list) {
    let sum = 0;
    let curr = list.head;
 
 
    while (curr !== null) {
        sum += curr.data;
        curr = curr.next;
    }
    return sum;
}
 
 
let list = new LinkedList();
list.add(3);
list.add(8);
list.add(3);
list.add(1);
let total = sum(list);
console.log("Sum of numbers in the list: " + total);


Output

Sum of numbers in the list: 15

Time Complexity: O(n)

Space Complexity: O(1)

Using Recursive Approach

The code features a recursive strategy to calculate the sum of linked list elements, showcasing a function that calls itself. Within the recursive function, node values are added, and the process continues until the end of the linked list is reached. This recursive approach simplifies the code, making it easy to sum, and linked list elements.

Example: The example usage illustrates the practical application of this recursive method, creating a linked list and utilizing the function to find the sum of its elements.

Javascript




class Node {
    constructor(data) {
        this.data = data;
        this.next = null;
    }
}
 
class LinkedList {
    constructor() {
        this.head = null;
    }
 
    add(data) {
        let newNode = new Node(data);
        newNode.next = this.head;
        this.head = newNode;
    }
}
 
 
function sum(node) {
 
    if (node === null) {
        return 0;
    }
 
    return node.data + sum(node.next);
}
 
 
let list = new LinkedList();
list.add(7);
list.add(3);
list.add(8);
list.add(2);
let total = sum(list.head);
console.log("Sum of numbers using recursion: " + total);


Output

Sum of numbers using recursion: 20

Time Complexity: O(n)

Space Complexity: O(n), in this, and we are taking the stack space.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads