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 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 Javascript Program To Find The Sum Of Last N Nodes Of The Given Linked List Given a linked list and a number n. Find the sum of the last n nodes of the linked list.Constraints: 0 <= n <= number of nodes in the linked list.Examples: Input : 10->6->8->4->12, n = 2 Output : 16 Sum of last two nodes: 12 + 4 = 16 Input : 15->7->9->5->16->14, n = 10 min read Java Program to Compute the Sum of Numbers in a List Using For-Loop Given a list of numbers, write a Java program to find the sum of all the elements in the List using for loop. For performing the given task, complete List traversal is necessary which makes the Time Complexity of the complete program to O(n), where n is the length of the List. Example: Input : List 2 min read Javascript Program To Subtract Two Numbers Represented As Linked Lists Given two linked lists that represent two large positive numbers. Subtract the smaller number from the larger one and return the difference as a linked list. Note that the input lists may be in any order, but we always need to subtract smaller from the larger ones.It may be assumed that there are no 5 min read Java Program to Create a Singly Linked List and Count the Number of Nodes Linked List is a linear data structure. Linked list elements are not stored at a contiguous location, the elements are linked using pointers. Singly Linked list is the collection of nodes, where each node has two parts one is the data and other is the linked part. Example: Input : AddNodes = {2, 3, 3 min read Javascript Program To Multiply Two Numbers Represented By Linked Lists Given two numbers represented by linked lists, write a function that returns the multiplication of these two linked lists.Examples: Input: 9->4->6 8->4Output: 79464Input: 3->2->1 1->2Output: 3852Solution: Traverse both lists and generate the required numbers to be multiplied and th 2 min read Like