Find maximum pairwise sum in Linked List that are equidistant from front and back
Last Updated :
09 Mar, 2023
Given a linked list lis of length N, where N is even. The task is to maximize the sum of two equidistant nodes from the front and back ends of the given linked list.
Note: Two nodes (i and j) are equidistant from both ends if the distance of ith node from front is same as distance of jth node from back.
Examples:
Input: lis = {5, 4, 2, 1}
Output: 6
Explanation: The nodes with pairs present in this linked list are:
Node 0 and node 3 are equidistant having a sum of 5 + 1 = 6.
Node 1 and node 2 are equidistant having a sum of 4 + 2 = 6.
Thus, the maximum sum of equidistant nodes of the linked list is max(6, 6) = 6.
Input: lis = {4, 2, 2, 3}
Output: 7
Explanation: The nodes with pairs present in this linked list are:
Node 0 and node 3 are equidistant having a sum of 4 + 3 = 7.
Node 1 and node 2 are equidistant having a sum of 2 + 2 = 4.
Thus, the maximum sum of equidistant nodes of the linked list is max(7, 4) = 7.
Approach: The solution is based on dividing the linked list into two equal halves and then using two pointer approach. Follow the steps mentioned below to solve the problem:
- Get mid and separate the linked list into two parts.
- Reverse the second part, for traversing it in the forward direction.
- Traverse in both parts and get the maximum sum.
- Recover the Linked List Again, by connecting the parts again, for good practice.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
struct ListNode {
int val;
ListNode* next;
ListNode()
: val(0), next(nullptr)
{
}
ListNode( int x)
: val(x), next(nullptr)
{
}
ListNode( int x, ListNode* next)
: val(x), next(next)
{
}
};
void push( struct ListNode** head_ref,
int new_data)
{
struct ListNode* new_node
= new ListNode;
new_node->val = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void reverse(ListNode** head)
{
ListNode *curr = *head, *prev = 0, *nxt;
while (curr)
nxt = curr->next,
curr->next = prev,
prev = curr,
curr = nxt;
*head = prev;
}
int pairSum(ListNode* head)
{
ListNode *prev = 0, *slow = head,
*fast = head;
while (fast and fast->next)
prev = slow, slow = slow->next,
fast = fast->next->next;
prev->next = 0;
reverse(&slow);
int sum = 0;
ListNode *ptr1 = head, *ptr2 = slow;
while (ptr1)
sum = max(sum, (ptr1->val
+ ptr2->val)),
ptr1 = ptr1->next, ptr2
= ptr2->next;
reverse(&slow);
prev->next = slow;
return sum;
}
int main()
{
struct ListNode* head = NULL;
push(&head, 4);
push(&head, 2);
push(&head, 2);
push(&head, 3);
cout << pairSum(head);
return 0;
}
|
Java
class GFG{
static class ListNode {
int val;
ListNode next;
ListNode()
{
this ( 0 );
}
ListNode( int x)
{
this .val = x;
this .next = null ;
}
ListNode( int x, ListNode next)
{
this .val = x;
this .next = next;
}
};
static ListNode push(ListNode head_ref,
int new_data)
{
ListNode new_node
= new ListNode();
new_node.val = new_data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
static ListNode reverse(ListNode head)
{
ListNode curr = head, prev = new ListNode(), nxt= new ListNode();
while (curr.next!= null ) {
nxt = curr.next;
curr.next = prev;
prev = curr;
curr = nxt;
}
head = prev;
return head;
}
static int pairSum(ListNode head)
{
ListNode prev = new ListNode(), slow = head,
fast = head;
while (fast!= null && fast.next!= null ) {
prev = slow;
slow = slow.next;
fast = fast.next.next;
}
prev.next = new ListNode();
slow = reverse(slow);
int sum = 0 ;
ListNode ptr1 = head, ptr2 = slow;
while (ptr1!= null ) {
sum = Math.max(sum, (ptr1.val
+ ptr2.val));
ptr1 = ptr1.next;
ptr2 = ptr2.next;
}
slow = reverse(slow);
prev.next = slow;
return sum;
}
public static void main(String[] args)
{
ListNode head = new ListNode();
head = push(head, 4 );
head = push(head, 2 );
head = push(head, 2 );
head = push(head, 3 );
System.out.print(pairSum(head));
}
}
|
Python3
class ListNode:
def __init__( self , val = 0 , next = None ):
self .val = val
self . next = next
def push(head_ref, new_data):
new_node = ListNode(new_data)
new_node. next = head_ref
head_ref = new_node
return head_ref
def reverse(head):
curr = head
prev = None
nxt = None
while curr. next is not None :
nxt = curr. next
curr. next = prev
prev = curr
curr = nxt
head = prev
return head
def pairSum(head):
prev = None
slow = head
fast = head
while fast is not None and fast. next is not None :
prev = slow
slow = slow. next
fast = fast. next . next
prev. next = None
slow = reverse(slow)
sum = 0
ptr1 = head
ptr2 = slow
while ptr1 is not None :
sum = max ( sum , (ptr1.val + ptr2.val))
ptr1 = ptr1. next
ptr2 = ptr2. next
slow = reverse(slow)
prev. next = slow
return sum
head = ListNode()
head = push(head, 4 )
head = push(head, 2 )
head = push(head, 2 )
head = push(head, 3 )
print (pairSum(head))
|
C#
using System;
public class GFG{
class ListNode {
public int val;
public ListNode next;
public ListNode()
{
new ListNode(0);
}
public ListNode( int x)
{
this .val = x;
this .next = null ;
}
public ListNode( int x, ListNode next)
{
this .val = x;
this .next = next;
}
};
static ListNode push(ListNode head_ref,
int new_data)
{
ListNode new_node
= new ListNode();
new_node.val = new_data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
static ListNode reverse(ListNode head)
{
ListNode curr = head, prev = new ListNode(), nxt= new ListNode();
while (curr.next!= null ) {
nxt = curr.next;
curr.next = prev;
prev = curr;
curr = nxt;
}
head = prev;
return head;
}
static int pairSum(ListNode head)
{
ListNode prev = new ListNode(), slow = head,
fast = head;
while (fast!= null && fast.next!= null ) {
prev = slow;
slow = slow.next;
fast = fast.next.next;
}
prev.next = new ListNode();
slow = reverse(slow);
int sum = 0;
ListNode ptr1 = head, ptr2 = slow;
while (ptr1!= null ) {
sum = Math.Max(sum, (ptr1.val
+ ptr2.val));
ptr1 = ptr1.next;
ptr2 = ptr2.next;
}
slow = reverse(slow);
prev.next = slow;
return sum;
}
public static void Main(String[] args)
{
ListNode head = new ListNode();
head = push(head, 4);
head = push(head, 2);
head = push(head, 2);
head = push(head, 3);
Console.Write(pairSum(head));
}
}
|
Javascript
class ListNode {
constructor(x = null , next = null ) {
this .val = x;
this .next = next;
}
};
function push(head_ref, new_data) {
let new_node = new ListNode();
new_node.val = new_data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
function reverse(head) {
let curr = head, prev = new ListNode(), nxt = new ListNode();
while (curr.next != null ) {
nxt = curr.next;
curr.next = prev;
prev = curr;
curr = nxt;
}
head = prev;
return head;
}
function pairSum(head) {
let prev = new ListNode(), slow = head, fast = head;
while (fast != null && fast.next != null ) {
prev = slow;
slow = slow.next;
fast = fast.next.next;
}
prev.next = new ListNode();
slow = reverse(slow);
let sum = 0;
let ptr1 = head, ptr2 = slow;
while (ptr1 != null ) {
sum = Math.max(sum, (ptr1.val
+ ptr2.val));
ptr1 = ptr1.next;
ptr2 = ptr2.next;
}
slow = reverse(slow);
prev.next = slow;
return sum;
}
let head = new ListNode();
head = push(head, 4);
head = push(head, 2);
head = push(head, 2);
head = push(head, 3);
console.log(pairSum(head));
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...