# Largest and Smallest Element in a Singly Linked List

Riya
Last Updated: May 13, 2022

## Introduction-

This article will discuss the problem of finding the “Largest and Smallest Element in a Singly Linked List” and its solution. Before jumping into the details of the problem and approach to solve it, you need to know about the linked list data structure.

Now, let’s understand the problem. In this problem, a singly linked list is given, and we have to find the value of the largest and smallest element in a singly linked list that is given in the problem.

Let’s take an example to understand it:

Assume the given linked list is : 1 -> 4 -> 5 -> 9 -> 8 -> 5 -> 2

All the values of the nodes present in the linked list are 1, 4, 5, 9, 8, 5, 2. Here we can see that 9 and 1 are the largest and smallest elements respectively among all the nodes’ values.

So, our code should return 9 and 1 as the largest and smallest element in a Singly linked list which is taken in the above example.

## Solution Approach

We have to find the largest and smallest element in a singly linked list, so our solution approach is to traverse each node of the singly linked list and get the largest and smallest of all the nodes’ values.

## Algorithm -

Step 1. Create a class ‘Node’ for creating the linked lists.

Step 2. Then create the function “largestAndSmallest()” to print the largest and smallest element in a singly linked list, which will accept one parameter - pointer to the head node of the given linked list.

Step 3.  Initialize two variables, “smallestElement = INT_MAX” and “largestElement = INT_MIN”, to store the current smallest and largest elements, respectively, after traversing each node of the given singly linked list.

Step 4. Initialize a pointer to the node as “curr_node = head” and traverse the whole linked list with the help of this pointer in a while loop. Compare the value of the current node with “smallestElement” and “largestElement” and update them.

Step 5. Finally, after traversing the linked list, print the values of the smallest and largest elements in the given singly linked list.

## Algorithm Complexity:

Time Complexity: O(N)

In the function “largestAndSmallest()”, we have created a “while loop” to one by one traverse all the nodes of the given linked list. So, the time complexity is O(N), where ‘N’ is the number of nodes in the given linked list.

Space Complexity: O(1)

As we have used constant space, so the space complexity is O(1)

1. What is a linked list?

A linked list is a linear data structure that is formed by connected nodes. Each node has a value associated with it and the pointer(s) to its directly connected node(s).

2. What is the key difference between a singly linked list and a doubly-linked list?

A singly-linked list is unidirectional, which means that the node of a singly linked list contains the pointer to its next node only. In contrast, a doubly-linked list is bidirectional, and its node contains the pointer to its next and previous nodes.

## Key takeaways-

This article discussed the problem of finding the “Largest and Smallest Element in a Singly Linked List”, approach to solve the problem, its C++ implementation, and its time and space complexities. If you want to practice problems on a linked list, then you can visit codestudio.

If you think that this blog helped you, then share it with your friends!. Refer to the DSA C++ course for more information.

Until then, All the best for your future endeavors, and Keep Coding.