Sum of the nodes of a Singly Linked ListGiven a singly linked list. The task is to find the sum of nodes of the given linked list. Show Examples: Recommended: Please try your approach on {IDE} first, before moving on to the solution. Recursive Solution:
Below is the implementation of above approach: C++
Java
Python3
C#
Javascript
Output:
Sum of nodes = 26
Time Complexity: O(N) , N is the number of nodes in a linked list.
Below is the implementation of above approach: C++
Java
Python3
C#
Javascript
Output:
Sum of nodes = 26
Time Complexity: O(N), N is the number of nodes in a linked list.
Article Tags :
Linked List
Algorithms-Recursion Data Structures-Linked List Traversal Practice Tags :
Linked List Traversal Sum of the nodes of a Singly Linked List in C ProgramCServer Side ProgrammingProgramming A singly linked list is a data structure in which an element has two parts one is the value and other is the link to the next element. So to find the sum of all elements of the singly linked list, we have to navigate to each node of the linked list and add the element's value to a sum variable. For example Suppose we have a linked list: 2 -> 27 -> 32 -> 1 -> 5 sum = 2 + 27 + 32 + 1 + 5 = 67.This can be done by using two methods : Method 1- Using a loop that loops over all the values of the linked list and finds the sum. The loop runs till the end of the linked list i.e. when the pointer of an element points to null, this loop will run and find the sum of values of each element. Linked List Operations: Traverse, Insert and DeleteIn this tutorial, you will learn different operations on a linked list. Also, you will find implementation of linked list operations in C/C++, Python and Java. There are various linked list operations that allow us to perform different actions on linked lists. For example, the insertion operation adds a new element to the linked list. Here's a list of basic linked list operations that we will cover in this article.
Before you learn about linked list operations in detail, make sure to know about Linked List first. Things to Remember about Linked List
In all of the examples, we will assume that the linked list has three nodes 1 --->2 --->3 with node structure as below: struct node { int data; struct node *next; };Program to create a singly linked list of n nodes and count the number of nodesExplanationIn this program, we need to create a singly linked list and count the nodes present in the list. To accomplish this task, traverse through the list using node current which initially points to head. Increment current in such a way that current will point to its next node in each iteration and increment variable count by 1. In the end, the count will hold the value which denotes the number of nodes present in the list. Algorithm
SolutionPythonOutput: Nodes of singly linked list:
1 2 3 4
Count of nodes present in the list: 4
Output: Nodes of singly linked list:
1 2 3 4
Count of nodes present in the list: 4
JAVAOutput: Nodes of the singly linked list:
1 2 3 4
Count of nodes present in the list: 4
C#Output: Nodes of singly linked list:
1 2 3 4
Count of nodes present in the list: 4
PHPOutput: Nodes of singly linked list:
1 2 3 4
Count of nodes present in the list: 4
LinkedList representationEach element in the LinkedList is called the Node. Each Node of the LinkedList contains two items: 1) Content of the element 2) Pointer/Address/Reference to the Next Node in the LinkedList. This is how a LinkedList looks: Note: |