Find smallest and largest elements in singly linked list

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

Examples:

Input : 15 14 13 22 17
Output : Linked list are:
        17 -> 22 -> 13 -> 14 -> 15 -> NULL
        Maximum element in linked list: 22
        Minimum element in linked list: 13

Input : 20 25 23 68 54 13 45
Output : Linked list are:
        45 -> 13 -> 54 -> 68 -> 23 -> 25 -> 20 -> NULL
        Maximum element in linked list: 68
        Minimum element in linked list: 13

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.

// Program to find 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;
}

// Display linked list.
void printList(struct Node* head)
{
    while (head != NULL) {
        printf("%d -> ", head->data);
        head = head->next;
    }
    cout << "NULL" << endl;
}

// 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 << "Linked list is : " << endl;

    // Call printList() function to display
    // the linked list.
    printList(head);
    cout << "Maximum element in linked list:";

    // Call largestElement() function to get largest
    // element in linked list.
    cout << largestElement(head) << endl;
    cout << "Minimum element in linked list:";

    // Call smallestElement() function to get smallest
    // element in linked list.
    cout << smallestElement(head) << endl;

    return 0;
}

Output:

Linked list is :
17 -> 22 -> 13 -> 14 -> 15 -> NULL
Maximum element in linked list: 22
Minimum element in linked list: 13

This article is contributed by Dharmendra kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



0 Average Difficulty : 0/5.0
No votes yet.










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.