Find maximum absolute difference between weights of adjacent nodes in Linked list
Last Updated :
11 Nov, 2023
Given a linked list with weights, the task is to find the maximum absolute difference between any two weights of nodes that are adjacent in the list.
Examples:
Input: 2 (4) -> 5 (8) -> 6 (1) -> 4 (3) -> 7 (2) -> NULL
Output: 7
Explanation: The maximum absolute difference between any two weights of nodes that are adjacent in the list is 7 (8 – 1) between nodes 5 and 6.
Input: 4 (1) -> 8 (7) -> 2 (2) -> 6 (4) -> NULL
Output: 6
Explanation: The maximum difference between any two weights of nodes that are adjacent in the list is 6 (1 – 7) between nodes 4 and 8.
Approach: To solve the problem follow the below idea:
Traverse the Linked List and take the difference of adjacent elements in the list.
Here are the steps of the above approach:
- First, we define a function max_adjacent_diff to find the maximum absolute difference between any two adjacent nodes in the Linked list. The function takes the head of the list as input and returns an integer representing the maximum difference.
- Check if the list is empty or contains only one node. If so, we return 0 because there are no adjacent nodes to compare.
- Then we initialize a variable max_diff to hold the maximum difference seen so far to the absolute difference between the weights of the first two nodes in the list.
- Define a pointer curr to point to the second node in the list, as we have already compared the weights of the first two nodes.
- Then we iterate through the list, comparing the weight of each node with the weight of its next node. If the absolute difference between the weights of the two nodes is greater than the maximum difference seen so far, we update the maximum difference to the new difference.
- Once we have iterated through the entire list, we return the maximum absolute difference.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int val;
int weight;
Node* next;
};
void push(Node** head, int val, int weight)
{
Node* new_node = new Node;
new_node->val = val;
new_node->weight = weight;
new_node->next = nullptr;
if (*head == nullptr) {
*head = new_node;
return ;
}
Node* last = *head;
while (last->next != nullptr) {
last = last->next;
}
last->next = new_node;
}
int max_adjacent_diff(Node* head)
{
if (!head || !head->next) {
return 0;
}
int max_diff = abs (head->weight - head->next->weight);
Node* curr = head->next;
while (curr->next) {
int diff = abs (curr->weight - curr->next->weight);
if (diff > max_diff) {
max_diff = diff;
}
curr = curr->next;
}
return max_diff;
}
int main()
{
Node* head = nullptr;
push(&head, 2, 4);
push(&head, 5, 8);
push(&head, 6, 1);
push(&head, 4, 3);
push(&head, 7, 2);
int max_diff = max_adjacent_diff(head);
cout << "Maximum difference between weights of "
"adjacent nodes: "
<< max_diff << endl;
return 0;
}
|
Java
class Node {
int val;
int weight;
Node next;
Node( int val, int weight) {
this .val = val;
this .weight = weight;
this .next = null ;
}
}
class Main {
static Node push(Node head, int val, int weight) {
Node newNode = new Node(val, weight);
if (head == null ) {
head = newNode;
return head;
}
Node last = head;
while (last.next != null ) {
last = last.next;
}
last.next = newNode;
return head;
}
static int maxAdjacentDiff(Node head) {
if (head == null || head.next == null ) {
return 0 ;
}
int maxDiff = Math.abs(head.weight - head.next.weight);
Node curr = head.next;
while (curr.next != null ) {
int diff = Math.abs(curr.weight - curr.next.weight);
if (diff > maxDiff) {
maxDiff = diff;
}
curr = curr.next;
}
return maxDiff;
}
public static void main(String[] args) {
Node head = null ;
head = push(head, 2 , 4 );
head = push(head, 5 , 8 );
head = push(head, 6 , 1 );
head = push(head, 4 , 3 );
head = push(head, 7 , 2 );
int maxDiff = maxAdjacentDiff(head);
System.out.println( "Maximum difference between weights of adjacent nodes: " + maxDiff);
}
}
|
Python
class Node:
def __init__( self , val, weight):
self .val = val
self .weight = weight
self . next = None
def push(head, val, weight):
new_node = Node(val, weight)
if head is None :
head = new_node
return head
last = head
while last. next :
last = last. next
last. next = new_node
return head
def max_adjacent_diff(head):
if not head or not head. next :
return 0
max_diff = abs (head.weight - head. next .weight)
curr = head. next
while curr. next :
diff = abs (curr.weight - curr. next .weight)
if diff > max_diff:
max_diff = diff
curr = curr. next
return max_diff
if __name__ = = "__main__" :
head = None
head = push(head, 2 , 4 )
head = push(head, 5 , 8 )
head = push(head, 6 , 1 )
head = push(head, 4 , 3 )
head = push(head, 7 , 2 )
max_diff = max_adjacent_diff(head)
print ( "Maximum difference between weights of adjacent nodes:" , max_diff)
|
C#
using System;
class Node
{
public int val;
public int weight;
public Node next;
public Node( int val, int weight)
{
this .val = val;
this .weight = weight;
this .next = null ;
}
}
class Program
{
static Node Push(Node head, int val, int weight)
{
Node newNode = new Node(val, weight);
if (head == null )
{
head = newNode;
return head;
}
Node last = head;
while (last.next != null )
{
last = last.next;
}
last.next = newNode;
return head;
}
static int MaxAdjacentDiff(Node head)
{
if (head == null || head.next == null )
{
return 0;
}
int maxDiff = Math.Abs(head.weight - head.next.weight);
Node curr = head.next;
while (curr.next != null )
{
int diff = Math.Abs(curr.weight - curr.next.weight);
if (diff > maxDiff)
{
maxDiff = diff;
}
curr = curr.next;
}
return maxDiff;
}
static void Main()
{
Node head = null ;
head = Push(head, 2, 4);
head = Push(head, 5, 8);
head = Push(head, 6, 1);
head = Push(head, 4, 3);
head = Push(head, 7, 2);
int maxDiff = MaxAdjacentDiff(head);
Console.WriteLine( "Maximum difference between weights of adjacent nodes: " + maxDiff);
}
}
|
Javascript
class Node {
constructor(val, weight) {
this .val = val;
this .weight = weight;
this .next = null ;
}
}
function push(head, val, weight) {
const new_node = new Node(val, weight);
if (head === null ) {
head = new_node;
return head;
}
let last = head;
while (last.next !== null ) {
last = last.next;
}
last.next = new_node;
return head;
}
function maxAdjacentDiff(head) {
if (!head || !head.next) {
return 0;
}
let maxDiff = Math.abs(head.weight - head.next.weight);
let curr = head.next;
while (curr.next) {
const diff = Math.abs(curr.weight - curr.next.weight);
if (diff > maxDiff) {
maxDiff = diff;
}
curr = curr.next;
}
return maxDiff;
}
let head = null ;
head = push(head, 2, 4);
head = push(head, 5, 8);
head = push(head, 6, 1);
head = push(head, 4, 3);
head = push(head, 7, 2);
const maxDiff = maxAdjacentDiff(head);
console.log(`Maximum difference between weights of adjacent nodes: ${maxDiff}`);
|
Output
Maximum difference between weights of adjacent nodes: 7
Time Complexity: O(n), where n is the number of nodes in the linked list.
Auxiliary Space: O(1), because we use a constant amount of extra space regardless of the size of the linked list.
Share your thoughts in the comments
Please Login to comment...