VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-maximum-pairwise-sum-in-linked-list-that-are-equidistant-from-front-and-back/

⇱ Find maximum pairwise sum in Linked List that are equidistant from front and back - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Find maximum pairwise sum in Linked List that are equidistant from front and back

Last Updated : 23 Jul, 2025

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.


Output
7

Time Complexity: O(N)
Auxiliary Space: O(1)

Comment