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)
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- 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 odd nodes of a 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 nodes with value as even digit sum in 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
- Remove all Fibonacci Nodes from a Circular Singly Linked List
- Remove all the Even Digit Sum Nodes from a Circular Singly Linked List
- Remove all even parity nodes from a Doubly and 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
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.