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)
- Splitting starting N nodes into new Circular Linked List while preserving the old nodes
- 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
- Delete all Prime Nodes from a Circular Singly Linked List
- Sum and Product of the nodes of a Circular Singly Linked List which are divisible by K
- 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
- Circular Linked List | Set 2 (Traversal)
- Deletion from a Circular Linked List
- Reverse a circular linked list
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.