JavaScript Program for Sum of Number Digits in a Linked List Last Updated : 23 Feb, 2024 Comments Improve Suggest changes Like Article Like Report We are going to write a JavaScript program on how to find the sum of number digits stored in a linked list. We are going to use the Iterative and Recursive techniques to find the Sum of number digits. A linked list is a data structure where elements are stored in nodes and each node points to the next node in the sequence. Table of Content Iterative MethodRecursive MethodIterative MethodIn this approach, we will traverse through each node of the linked list using a loop to extract the digits of the data and calculate their sum. Example: The below code uses a loop to iterate through each element of the linked list and get the sum of them. JavaScript class Node { constructor(data) { this.data = data; this.next = null; } } function sumOfDigitsIterative(head) { let sum = 0; while (head != null) { sum += head.data; head = head.next; } return sum; } const head = new Node(15); head.next = new Node(-20); head.next.next = new Node(35); console.log(`Total Sum: ${sumOfDigitsIterative(head)}`); OutputTotal Sum: 30 Recursive MethodIn this approach, we will create a recursive function which calls itself to traverse the linked list and calculate the sum of digits. Example: The below code uses the recursion method to iterate and get the sum of each element in the linked list. JavaScript class Node { constructor(data) { this.data = data; this.next = null; } } function sumOfDigitsRecursive(head) { if (head == null) { return 0; } const restSum = sumOfDigitsRecursive(head.next); return head.data + restSum; } const head = new Node(-12); head.next = new Node(13); head.next.next = new Node(15); console.log(`Total Sum: ${sumOfDigitsRecursive(head)}`); OutputTotal Sum: 16 Comment More infoAdvertise with us Next Article JavaScript Program for Sum of Number Digits in a Linked List J jaimin78 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Program Similar Reads JavaScript Program for Sum of Digits of a Number In this article, we are going to learn about finding the Sum of Digits of a number using JavaScript. The Sum of Digits refers to the result obtained by adding up all the individual numerical digits within a given integer. It's a basic arithmetic operation. This process is repeated until a single-dig 3 min read JavaScript Program to Find Sum of Elements in Linked List 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 4 min read JavaScript Program for Sum of Digits of a Number using Recursion We are given a number as input and we have to find the sum of all the digits contained by it. We will split the digits of the number and add them together using recursion in JavaScript. Table of Content Recursively Summing DigitsUsing string manipulation with recursionRecursively Summing DigitsIn th 2 min read Javascript Program For Adding 1 To A Number Represented As Linked List Number is represented in linked list such that each digit corresponds to a node in linked list. Add 1 to it. For example 1999 is represented as (1-> 9-> 9 -> 9) and adding 1 to it should change it to (2->0->0->0) Below are the steps : Reverse given linked list. For example, 1-> 5 min read Javascript Program To Find Decimal Equivalent Of Binary Linked List Given a singly linked list of 0s and 1s find its decimal equivalent.Input: 0->0->0->1->1->0->0->1->0Output: 50 Input: 1->0->0Output: 4The decimal value of an empty linked list is considered as 0.Initialize the result as 0. Traverse the linked list and for each node, mul 3 min read Like