Sum and product of smallest and largest elements in a linked list

Updated

Given a singly linked list of n nodes and find the sum and product of smallest and largest elements in linked list.

Examples :

Input : 15 14 13 22 17
Output : Sum = 35
         Product = 286
        Maximum element in linked list: 22
        Minimum element in linked list: 13

Input : 20 25 23 68 54 13 45
Output : Sum = 81
        Product = 884
        Maximum element in linked list: 68
        Minimum element in linked list: 13

Method (Simple Approach) :

The idea is to traverse the linked list while head not equal to NULL and initialise the max and min variable to INT_MIN and INT_MAX respectively. After that check a condition that if max value is less then head value then head value is assign to max or min value is greater then head value then head value is assign to min otherwise head point to next node. Continue this process until head not equal to NULL and print the sum and product of Minimum and Maximum element.

Implementation :

// Program to find the sum and product of smallest and largest 
// elements in singly linked list. 
#include <bits/stdc++.h> 
  
using namespace std; 
/* Linked list node */
struct Node { 
    int data; 
    struct Node* next; 
}; 
  
// Function that returns the largest element 
// from the linked list. 
int largestElement(struct Node* head) 
{ 
    // Declare a max variable and initialize 
    // it with INT_MIN value. 
    // INT_MIN is integer type and its value 
    // is -32767 or less. 
    int max = INT_MIN; 
  
    // Check loop while head not equal to NULL 
    while (head != NULL) { 
  
        // If max is less then head->data then 
        // assign value of head->data to max 
        // otherwise node point to next node. 
        if (max < head->data) 
            max = head->data; 
        head = head->next; 
    } 
    return max; 
} 
  
// Function that returns smallest element 
// from the linked list. 
int smallestElement(struct Node* head) 
{ 
    // Declare a min variable and initialize 
    // it with INT_MAX value. 
    // INT_MAX is integer type and its value 
    // is 32767 or greater. 
    int min = INT_MAX; 
  
    // Check loop while head not equal to NULL 
    while (head != NULL) { 
  
        // If min is greater then head->data then 
        // assign value of head->data to min 
        // otherwise node point to next node. 
        if (min > head->data) 
            min = head->data; 
  
        head = head->next; 
    } 
    return min; 
} 
  
// Function that push the element in linked list. 
void push(struct Node** head, int data) 
{ 
    // Allocate dynamic memory for newNode. 
    struct Node* newNode =  
         (struct Node*)malloc(sizeof(struct Node)); 
  
    // Assign the data into newNode. 
    newNode->data = data; 
  
    // newNode->next assign the address of 
    // head node. 
    newNode->next = (*head); 
  
    // newNode become the headNode. 
    (*head) = newNode; 
} 
  
// Drier program to test the functions 
int main() 
{ 
    // Start with empty list 
    struct Node* head = NULL; 
  
    // Using push() function to construct 
    // singly linked list 
    // 17->22->13->14->15 
    push(&head, 15); 
    push(&head, 14); 
    push(&head, 13); 
    push(&head, 22); 
    push(&head, 17); 
    cout << "Sum of Maximum element and Minimum element in linked list:"; 
  
    // Call largestElement() and smallestElement() function to get sum
    cout << largestElement(head)+smallestElement(head) << endl; 
 cout << "Product of Maximum element and Minimum element in linked list:"; 
    // Call largestElement() and smallestElement() function to get product
    cout << largestElement(head)*smallestElement(head) << endl; 
    return 0; 
} 
Comment
RKplus Published 14 Nov 2026 · 3 min read

Related tutorials