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:
// Java Implementation #include <bits/stdc++.h> using namespace std;
// Node class class Node {
public :
int data;
int weight;
Node* next;
Node( int data, int weight)
{
this ->data = data;
this ->weight = weight;
next = NULL;
}
}; // Function to find product of nodes with // minimum and maximum weights 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;
// Traverse the linked list to find
// the nodes with minimum and
// maximum weights
while (curr != NULL) {
if (curr->weight < minWeight) {
minWeight = curr->weight;
minNode = curr;
}
if (curr->weight > maxWeight) {
maxWeight = curr->weight;
maxNode = curr;
}
curr = curr->next;
}
// Multiply the data of the nodes
// with minimum and maximum weight
int product = minNode->data * maxNode->data;
return product;
} // Driver code int main()
{ // Create the linked list
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);
// Function call
int product = findProduct(head);
cout << product << endl;
return 0;
} |
// Node class 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 {
// Function to find product of nodes with
// minimum and maximum weights
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 ;
// Traverse the linked list to find
// the nodes with minimum and
// maximum weights
while (curr != null ) {
if (curr.weight < minWeight) {
minWeight = curr.weight;
minNode = curr;
}
if (curr.weight > maxWeight) {
maxWeight = curr.weight;
maxNode = curr;
}
curr = curr.next;
}
// Multiply the data of the nodes
// with minimum and maximum weight
int product = minNode.data * maxNode.data;
return product;
}
// Driver code
public static void main(String[] args) {
// Create the linked list
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 );
// Function call
int product = findProduct(head);
System.out.println(product);
}
} // This code was contributed by codearcade |
# Python Implementation # Node class class Node:
def __init__( self , data, weight):
self .data = data
self .weight = weight
self . next = None
# Function to find product of nodes with minimum # and maximum weights def findProduct(head):
if head is None :
return 0
minWeight = float ( 'inf' )
maxWeight = float ( '-inf' )
curr = head
minNode = None
maxNode = None
# Traverse the linked list to find the nodes with
# minimum and maximum weights
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
# Multiply the data of the nodes with minimum
# and maximum weight
product = minNode.data * maxNode.data
return product
# Driver code if __name__ = = "__main__" :
# Create the linked list
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 )
# Function call
product = findProduct(head)
print (product)
|
using System;
// Node class 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
{ // Function to find product of nodes with
// minimum and maximum weights
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 ;
// Traverse the linked list to find
// the nodes with minimum and
// maximum weights
while (curr != null )
{
if (curr.weight < minWeight)
{
minWeight = curr.weight;
minNode = curr;
}
if (curr.weight > maxWeight)
{
maxWeight = curr.weight;
maxNode = curr;
}
curr = curr.next;
}
// Multiply the data of the nodes
// with minimum and maximum weight
int product = minNode.data * maxNode.data;
return product;
}
// Driver code
public static void Main( string [] args)
{
// Create the linked list
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);
// Function call
int product = FindProduct(head);
Console.WriteLine(product);
}
} // This code was contributed by Sakshi |
// Nikunj Sonigara 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 ;
// Traverse the linked list to find
// the nodes with minimum and maximum weights
while (curr !== null ) {
if (curr.weight < minWeight) {
minWeight = curr.weight;
minNode = curr;
}
if (curr.weight > maxWeight) {
maxWeight = curr.weight;
maxNode = curr;
}
curr = curr.next;
}
// Multiply the data of the nodes
// with minimum and maximum weight
let product = minNode.data * maxNode.data;
return product;
} // Driver code function main() {
// Create the linked list
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);
// Function call
let product = findProduct(head);
console.log(product);
} main(); |
96
Time Complexity: O(n)
Auxiliary Space: O(1)