Find product of nodes with min and max weights in a singly linked list
Last Updated :
15 Sep, 2023
Given a singly linked list with weights, the task is to find the product of nodes with minimum and maximum weights in the list.
Examples:
Input: 3 (4) -> 1 (2) -> 8 (14) -> 12 (1) -> 5 (9) -> 7 (3) -> NULL
Output: 96
Explanation: Node with minimum weight is 12 and maximum weight is 8 and so the output is 12 * 8 = 96.
Input: 2 (10) -> 4 (5) -> 6 (7) -> 8 (1) -> 1 (15) -> NULL
Output: 8
Explanation: Node with minimum weight is 8 and maximum weight is 1 and so the output is 8 * 1 = 8.
Approach: This can be solved with the following idea:
To solve this problem, we need to first find the nodes with the minimum and maximum weights. To do this, we traverse the linked list from the head node and keep track of the minimum and maximum weight seen so far. For each node, we compare its weight with the current minimum and maximum weights and update them accordingly. We also keep track of the node corresponding to the minimum and maximum weight seen so far. Once we have found the nodes with the minimum and maximum weights, we can simply multiply their data to get the desired product. We return this product as the final output.
Steps of the above approach:
- Initialize two variables ‘minWeight’ and ‘maxWeight’ to the maximum and minimum integer values respectively.
- Initialize two pointers ‘minNode’ and ‘maxNode’ to NULL.
- Traverse the linked list from head to tail.
- For each node, compare its weight with the current values of ‘minWeight’ and ‘maxWeight’.
- If the weight of the node is less than ‘minWeight’, update ‘minWeight’ to the weight of the node and ‘minNode’ to point to the current node.
- If the weight of the node is greater than ‘maxWeight’, update ‘maxWeight’ to the weight of the node and ‘maxNode’ to point to the current node.
- Once the traversal is complete, multiply the data of ‘minNode’ and ‘maxNode’ to get the desired product.
- Return the product as the final output.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
class Node {
public :
int data;
int weight;
Node* next;
Node( int data, int weight)
{
this ->data = data;
this ->weight = weight;
next = NULL;
}
};
int findProduct(Node* head)
{
if (head == NULL) {
return 0;
}
int minWeight = INT_MAX;
int maxWeight = INT_MIN;
Node* curr = head;
Node* minNode = NULL;
Node* maxNode = NULL;
while (curr != NULL) {
if (curr->weight < minWeight) {
minWeight = curr->weight;
minNode = curr;
}
if (curr->weight > maxWeight) {
maxWeight = curr->weight;
maxNode = curr;
}
curr = curr->next;
}
int product = minNode->data * maxNode->data;
return product;
}
int main()
{
Node* head = new Node(3, 4);
head->next = new Node(1, 2);
head->next->next = new Node(8, 14);
head->next->next->next = new Node(12, 1);
head->next->next->next->next = new Node(5, 9);
head->next->next->next->next->next = new Node(7, 3);
int product = findProduct(head);
cout << product << endl;
return 0;
}
|
Java
class Node {
public int data;
public int weight;
public Node next;
public Node( int data, int weight) {
this .data = data;
this .weight = weight;
this .next = null ;
}
}
public class FindProductLinkedList {
static int findProduct(Node head) {
if (head == null ) {
return 0 ;
}
int minWeight = Integer.MAX_VALUE;
int maxWeight = Integer.MIN_VALUE;
Node curr = head;
Node minNode = null ;
Node maxNode = null ;
while (curr != null ) {
if (curr.weight < minWeight) {
minWeight = curr.weight;
minNode = curr;
}
if (curr.weight > maxWeight) {
maxWeight = curr.weight;
maxNode = curr;
}
curr = curr.next;
}
int product = minNode.data * maxNode.data;
return product;
}
public static void main(String[] args) {
Node head = new Node( 3 , 4 );
head.next = new Node( 1 , 2 );
head.next.next = new Node( 8 , 14 );
head.next.next.next = new Node( 12 , 1 );
head.next.next.next.next = new Node( 5 , 9 );
head.next.next.next.next.next = new Node( 7 , 3 );
int product = findProduct(head);
System.out.println(product);
}
}
|
Python3
class Node:
def __init__( self , data, weight):
self .data = data
self .weight = weight
self . next = None
def findProduct(head):
if head is None :
return 0
minWeight = float ( 'inf' )
maxWeight = float ( '-inf' )
curr = head
minNode = None
maxNode = None
while curr is not None :
if curr.weight < minWeight:
minWeight = curr.weight
minNode = curr
if curr.weight > maxWeight:
maxWeight = curr.weight
maxNode = curr
curr = curr. next
product = minNode.data * maxNode.data
return product
if __name__ = = "__main__" :
head = Node( 3 , 4 )
head. next = Node( 1 , 2 )
head. next . next = Node( 8 , 14 )
head. next . next . next = Node( 12 , 1 )
head. next . next . next . next = Node( 5 , 9 )
head. next . next . next . next . next = Node( 7 , 3 )
product = findProduct(head)
print (product)
|
C#
using System;
public class Node
{
public int data;
public int weight;
public Node next;
public Node( int data, int weight)
{
this .data = data;
this .weight = weight;
this .next = null ;
}
}
public class FindProductLinkedList
{
static int FindProduct(Node head)
{
if (head == null )
{
return 0;
}
int minWeight = int .MaxValue;
int maxWeight = int .MinValue;
Node curr = head;
Node minNode = null ;
Node maxNode = null ;
while (curr != null )
{
if (curr.weight < minWeight)
{
minWeight = curr.weight;
minNode = curr;
}
if (curr.weight > maxWeight)
{
maxWeight = curr.weight;
maxNode = curr;
}
curr = curr.next;
}
int product = minNode.data * maxNode.data;
return product;
}
public static void Main( string [] args)
{
Node head = new Node(3, 4);
head.next = new Node(1, 2);
head.next.next = new Node(8, 14);
head.next.next.next = new Node(12, 1);
head.next.next.next.next = new Node(5, 9);
head.next.next.next.next.next = new Node(7, 3);
int product = FindProduct(head);
Console.WriteLine(product);
}
}
|
Javascript
class Node {
constructor(data, weight) {
this .data = data;
this .weight = weight;
this .next = null ;
}
}
function findProduct(head) {
if (head === null ) {
return 0;
}
let minWeight = Number.MAX_SAFE_INTEGER;
let maxWeight = Number.MIN_SAFE_INTEGER;
let curr = head;
let minNode = null ;
let maxNode = null ;
while (curr !== null ) {
if (curr.weight < minWeight) {
minWeight = curr.weight;
minNode = curr;
}
if (curr.weight > maxWeight) {
maxWeight = curr.weight;
maxNode = curr;
}
curr = curr.next;
}
let product = minNode.data * maxNode.data;
return product;
}
function main() {
let head = new Node(3, 4);
head.next = new Node(1, 2);
head.next.next = new Node(8, 14);
head.next.next.next = new Node(12, 1);
head.next.next.next.next = new Node(5, 9);
head.next.next.next.next.next = new Node(7, 3);
let product = findProduct(head);
console.log(product);
}
main();
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...