๐ Frontend DSA 5 - ๐ How to Sum Values in a Singly Linked List in JavaScript (Recursive Approach)
Singly linked lists are a fundamental data structure every JavaScript developer should understand. Theyโre used in everything from memory-efficient data storage to implementing queues and stacks. ๐
In this article, you'll learn how to traverse and sum all values in a singly linked list using a recursive approach in JavaScript. ๐ง
๐ Whatโs a Singly Linked List?
A singly linked list is a collection of nodes where:
- Each node has a
value
. - Each node has a reference (pointer) to the
next
node. - The last node's
next
isnull
.
Example:
1 โ 2 โ 3 โ 4 โ null
Each node "points" to the next, just like a chain ๐.
๐ง Problem: Calculate the Total Sum of All Values in the Linked List
Youโre given a linked list:
let list = {
value: 1,
next: {
value: 2,
next: {
value: 3,
next: {
value: 4,
next: null
}
}
}
};
Problem Statement
You need to write a function that sums all the value
s in the list and returns the total โ.
โ JavaScript Recursive Solution
// ๐ Function to calculate sum of linked list values recursively
const printList = (inputList) => {
// ๐ Base case: If no next node, return current node's value
if (inputList.next === null) {
return inputList.value;
} else {
// ๐ Recursive case: Add current value to sum of rest of list
return inputList.value + printList(inputList.next);
}
};
Code Block
๐ข Sample Input and Output
๐งช Example 1
let list1 = {
value: 1,
next: {
value: 2,
next: {
value: 3,
next: {
value: 4,
next: null
}
}
}
};
console.log(printList(list1)); // โ
Output: 10
Code Block
๐งช Example 2
let list2 = {
value: 5,
next: {
value: 10,
next: {
value: 15,
next: null
}
}
};
console.log(printList(list2)); // โ
Output: 30
Code Block
๐งช Example 3
let list3 = {
value: 42,
next: null
};
console.log(printList(list3)); // โ
Output: 42
Code Block
๐ง JavaScript Notes
- Recursion is when a function calls itself. In our case, each function call processes one node and passes the rest down.
- The base case (
if(inputList.next === null)
) ensures the recursion stops. - This function returns a sum, not logs โ great for unit tests! โ
- Works well for small to medium-sized lists. For very large lists, consider an iterative version to avoid a stack overflow. โ ๏ธ
๐ Iterative Version (Bonus)
const printListIterative = (inputList) => {
let sum = 0;
let current = inputList;
while (current !== null) {
sum += current.value;
current = current.next;
}
return sum;
};
Code Block
โจ Final Thoughts
Learning to work with linked lists is ๐ for improving your data structure and algorithm skills in JavaScript. Whether you're preparing for interviews or building custom data models, recursive and iterative traversal will always be handy.
๐งโ๐ป Want to visualize the list traversal or convert this to a doubly linked list? Let me know โ weโll break it down step by step!
Hey there from LearnYard! ๐
Glad to see you exploring the free content from our course! Weโve shared 10 valuable articles at no cost to help you get a real feel of whatโs inside.
If youโve found these helpful, weโd love to know! ๐ฌ
Share your thoughts or key takeaways on LinkedIn, Twitter, or Instagram โ and donโt forget to tag Gourav Hammad & Mohammad Fraz. Weโll personally respond to your post!
Thanks for being part of the LearnYard journey. Keep learning, keep growing! ๐
Loved the free content? Want access to the full course โ for free? ๐ธ
Share the course with your friends using your affiliate link (youโll find it on your profile or home page), and earn 20% commission on each sale.
Just 4โ5 sales and youโll recover the full course price of your course ๐
So why wait? Start sharing, start earning, and unlock the rest of the course effortlessly! ๐ ๏ธ๐
๐ Keywords for SEO
- JavaScript Linked List Sum Example
- Recursively Traverse a Linked List in JavaScript
- Singly Linked List Recursion in JavaScript
- How to Sum Nodes in a Linked List Using JavaScript
- Beginnerโs Guide to Data Structures in JavaScript
- Linked List Implementation in JavaScript
- Recursive Function to Sum Linked List Nodes
- Understanding Recursion with Linked Lists in JS
- JavaScript Data Structures Tutorial
- Linked List Basics for JavaScript Beginners
๐ข Hashtags for Visibility
#JavaScriptDataStructures #LinkedListJS #RecursionInJS #FrontendInterviewPrep #LearnJavaScript #SinglyLinkedList #JSForBeginners #CodeNewbie #DSAwithJavaScript #JavaScriptRecursion #FrontendMasters #TechLearning #CodingChallenges #JSAlgorithms #SumLinkedList #RecursiveJS #WebDevelopment #InterviewPreparation #FunctionalProgrammingJS #JavaScriptEssentials