Given a singly linked list of nodes. Find the smallest and largest elements in linked list divisible by a given number .
Input : List = 15 -> 14 -> 13 -> 22 -> 50
K = 5
Maximum element in linked list divisible by K: 50
Minimum element in linked list divisible by K: 5
Input : List = 10 -> 14 -> 13 -> 22 -> 100
K = 10
Maximum element in linked list divisible by K: 100
Minimum element in linked list divisible by K: 10
- The idea is to traverse the linked list to the end and initialize the max and min variable to INT_MIN and INT_MAX respectively.
- After that check a condition that if the max value is less then the current node’s value and divisible by K then current node’s value is assigned to max.
- Similarly check if the current node’s value is less than min value and divisible by k then the current node’s value is assigned to min. Repeat above two steps until the end of the list is reached.
Below is the implementation of the above approach:
Max Element : 50 Min Element : 5
Time complexity : O(n)
- Minimum and Maximum element of an array which is divisible by a given number k
- Sum of all nodes in a doubly linked list divisible by a given number K
- Minimum and Maximum Prime Numbers of a Singly Linked List
- Product of all nodes in a doubly linked list divisible by a given number K
- Find minimum and maximum elements in singly Circular Linked List
- Create new linked list from two given linked list with greater element at each node
- Create a linked list from two linked lists by choosing max element at each position
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
- Sum and Product of the nodes of a Singly Linked List which are divisible by K
- Delete all nodes from the doubly linked list which are divisible by K
- Sum of (maximum element - minimum element) for all the subsets of an array.
- Sum and Product of the nodes of a Circular Singly Linked List which are divisible by K
- Move first element to end of a given Linked List
- Majority element in a linked list
- Second Smallest Element in a Linked List
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.