Sum of smaller elements of nodes in a linked list
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.
Examples:
Input: (2, 3) -> (3, 4) – > (1, 10) -> (20, 15) -> (7, 5) -> NULL
Output: 26
2 + 3 + 1 + 15 + 5 = 26
Input: (7, 3) -> (3, 9) -> (5, 10) -> (20, 8) -> (19, 11) -> NULL
Output: 30
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:
C++
#include <iostream>
using namespace std;
struct Node {
int first;
int second;
Node* next;
};
void insert(Node** head, int f, int s)
{
Node* ptr = *head;
Node* temp = new Node();
temp->first = f;
temp->second = s;
temp->next = NULL;
if (*head == NULL)
*head = temp;
else {
while (ptr->next != NULL)
ptr = ptr->next;
ptr->next = temp;
}
}
int calSum(Node* head)
{
int sum = 0;
while (head != NULL) {
if (head->first > head->second)
sum += head->second;
else
sum += head->first;
head = head->next;
}
return sum;
}
int main()
{
Node* head = NULL;
insert(&head, 2, 3);
insert(&head, 3, 4);
insert(&head, 1, 10);
insert(&head, 20, 15);
insert(&head, 7, 5);
cout << calSum(head) << endl;
return 0;
}
|
Java
class GFG {
static class Node {
int first;
int second;
Node next;
}
static Node insert(Node head, int f, int s)
{
Node ptr = head;
Node temp = new Node();
temp.first = f;
temp.second = s;
temp.next = null ;
if (head == null )
head = temp;
else {
while (ptr.next != null )
ptr = ptr.next;
ptr.next = temp;
}
return head;
}
static int calSum(Node head)
{
int sum = 0 ;
while (head != null ) {
if (head.first > head.second)
sum += head.second;
else
sum += head.first;
head = head.next;
}
return sum;
}
public static void main(String args[])
{
Node head = null ;
head = insert(head, 2 , 3 );
head = insert(head, 3 , 4 );
head = insert(head, 1 , 10 );
head = insert(head, 20 , 15 );
head = insert(head, 7 , 5 );
System.out.print(calSum(head));
}
}
|
Python3
class Node:
def __init__( self , f, s):
self .first = f
self .second = s
self . next = None
def insert(head, f, s):
ptr = head
temp = Node(f, s)
if head = = None :
head = temp
else :
while ptr. next ! = None :
ptr = ptr. next
ptr. next = temp
return head
def calSum(head):
Sum = 0
while head ! = None :
if head.first > head.second:
Sum + = head.second
else :
Sum + = head.first
head = head. next
return Sum
if __name__ = = "__main__" :
head = None
head = insert(head, 2 , 3 )
head = insert(head, 3 , 4 )
head = insert(head, 1 , 10 )
head = insert(head, 20 , 15 )
head = insert(head, 7 , 5 )
print (calSum(head))
|
C#
using System;
class GFG {
public class Node {
public int first;
public int second;
public Node next;
}
static Node insert(Node head, int f, int s)
{
Node ptr = head;
Node temp = new Node();
temp.first = f;
temp.second = s;
temp.next = null ;
if (head == null )
head = temp;
else {
while (ptr.next != null )
ptr = ptr.next;
ptr.next = temp;
}
return head;
}
static int calSum(Node head)
{
int sum = 0;
while (head != null ) {
if (head.first > head.second)
sum += head.second;
else
sum += head.first;
head = head.next;
}
return sum;
}
public static void Main(String[] args)
{
Node head = null ;
head = insert(head, 2, 3);
head = insert(head, 3, 4);
head = insert(head, 1, 10);
head = insert(head, 20, 15);
head = insert(head, 7, 5);
Console.Write(calSum(head));
}
}
|
Javascript
<script>
class Node {
constructor() {
this .first = 0;
this .second = 0;
this .next = null ;
}
}
function insert(head, f, s) {
var ptr = head;
var temp = new Node();
temp.first = f;
temp.second = s;
temp.next = null ;
if (head == null ) head = temp;
else {
while (ptr.next != null ) ptr = ptr.next;
ptr.next = temp;
}
return head;
}
function calSum(head) {
var sum = 0;
while (head != null ) {
if (head.first > head.second) sum += head.second;
else sum += head.first;
head = head.next;
}
return sum;
}
var head = null ;
head = insert(head, 2, 3);
head = insert(head, 3, 4);
head = insert(head, 1, 10);
head = insert(head, 20, 15);
head = insert(head, 7, 5);
document.write(calSum(head));
</script>
|
Time Complexity: O(N), where N is the number of nodes in the linkedlist.
Auxiliary Space: O(1)
Last Updated :
20 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...