Given a linked list, every node of which consists of a pair of integer variables first and second to hold the data, and a pointer pointing to the next node in the list. The task is to find the sum of min(first, second) for every node.
Input: (2, 3) -> (3, 4) – > (1, 10) -> (20, 15) -> (7, 5) -> NULL
2 + 3 + 1 + 15 + 5 = 26
Input: (7, 3) -> (3, 9) -> (5, 10) -> (20, 8) -> (19, 11) -> NULL
3 + 3 + 5 + 8 + 11 = 30
Approach: Traverse the whole linked list and for each node if the first element of node is smaller in comparison of second element of that node then we add first element to the variable which is holding the sum, otherwise we add the second element.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Delete all the nodes from a doubly linked list that are smaller than a given value
- 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
- Append odd position nodes in reverse at the end of even positioned nodes in a Linked List
- Splitting starting N nodes into new Circular Linked List while preserving the old nodes
- Sum and Product of all even digit sum Nodes of a Singly Linked List
- Sum and Product of nodes with value as even digit sum in Circular Linked List
- Replace even nodes of a doubly linked list with the elements of array
- XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
- Merge a linked list into another linked list at alternate positions
- Check if a linked list is Circular Linked List
- Convert singly linked list into circular linked list
- Difference between Singly linked list and Doubly linked list
- Convert Singly Linked List to XOR Linked List
- Create new linked list from two given linked list with greater element at each node
- XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
- Find the sum of last n nodes of the given Linked List
- Linked List Sum of Nodes Between 0s
- Sum of the nodes of a Singly Linked List
- Sum of all nodes in a doubly linked list divisible by a given number K
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.