Given a singly Circular linked list. The task is to find the sum of nodes of the given linked list.
For the above circular list, sum = 2 + 5 + 7 + 8 + 10 = 32
Input: 11->2->56->12 Output: Sum of Circular linked list is = 81 Input: 2-> 5 -> 7 -> 8 -> 10 Output: Sum of Circular linked list is = 32
- Initialize a pointer temp with the head of the linked list and a sum variable with 0.
Start traversing the linked list using a loop until all the nodes get traversed.
- Add the value of current node to the sum i.e. sum += temp -> data.
- Increment the pointer to the next node of linked list i.e. temp = temp -> next.
- Return the sum.
Below is the implementation of the above approach:
Sum of Circular linked list is = 81
Time Complexity: O(N) where N is the number of nodes in the linked list.
Auxiliary Space: O(1)
- Count nodes in Circular linked list
- Exchange first and last nodes in Circular Linked List
- Delete all the even nodes of a Circular Linked List
- Delete all odd or even positioned nodes from Circular Linked List
- Sum and Product of the nodes of a Circular Singly Linked List which are divisible by K
- Delete all Prime Nodes from a Circular Singly Linked List
- Circular Queue | Set 2 (Circular Linked List Implementation)
- Convert singly linked list into circular linked list
- Check if a linked list is Circular Linked List
- Append odd position nodes in reverse at the end of even positioned nodes in a Linked List
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
- Delete N nodes after M nodes of a linked list
- Deletion from a Circular Linked List
- Circular Linked List | Set 2 (Traversal)
- Reverse a circular linked list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.