
Problem Statement
In the context of this problem, we are dealing with a linked list of even size n
. Each node in the linked list has a corresponding "twin" node, where the twin of a node at position i
(where i
ranges from 0
to (n/2)-1
) is at position (n-1-i)
. This concept creates pairs of nodes whose values can be summed up to find the "twin sum". The task is to identify the pair that yields the maximum twin sum and return this maximum value. Given the head of such a linked list, the objective is to compute and return the largest possible twin sum from among all the twin pairs in the list.
Examples
Example 1
Input:
head = [5,4,2,1]
Output:
6
Explanation:
Nodes 0 and 1 are the twins of nodes 3 and 2, respectively. All have twin sum = 6. There are no other nodes with twins in the linked list. Thus, the maximum twin sum of the linked list is 6.
Example 2
Input:
head = [4,2,2,3]
Output:
7
Explanation:
The nodes with twins present in this linked list are: - Node 0 is the twin of node 3 having a twin sum of 4 + 3 = 7. - Node 1 is the twin of node 2 having a twin sum of 2 + 2 = 4. Thus, the maximum twin sum of the linked list is max(7, 4) = 7.
Example 3
Input:
head = [1,100000]
Output:
100001
Explanation:
There is only one node with a twin in the linked list having twin sum of 1 + 100000 = 100001.
Constraints
- The number of nodes in the list is an even integer in the range
[2, 105]
. 1 <= Node.val <= 105
Approach and Intuition
To solve this problem efficiently, it's important to understand the structure of twins and utilize the linked list's properties. Here's the step-by-step approach:
- Traverse half the linked list to reach the midpoint. Since the list size
n
is even, this is straightforward. The goal is to reach node(n/2)
. - As we traverse from the start to the midpoint, push the values of the nodes onto a stack. This will help in accessing the values of the first half of the list in reverse order.
- Once at the midpoint, continue traversing the second half of the list. For each node in the second half, pop a value from the stack (which represents the corresponding twin in the first half) and calculate the twin sum.
- Track the maximum twin sum encountered during the above step.
- The result after completing the traversal is the maximum twin sum.
This method ensures that we traverse the list only once and use a stack to handle the reversal efficiently, leading to an overall time complexity of O(n) and space complexity involving O(n/2) for the stack.
Using the examples:
- Example 1: Nodes 0, 1 are twins of nodes 3, 2 respectively, yielding twin sums all equaling 6. The maximum is evidently 6.
- Example 2: Node 0 is the twin of node 3 (sum: 7), and node 1 with node 2 (sum: 4). The maximum of these sums is 7.
- Example 3: Only two nodes present (maximum twin sum is straightforwardly 100001).
In each case, the procedure aligns perfectly with the steps outlined, confirming our approach's effectiveness.
Solutions
- C++
- Java
class Solution {
public:
int maxTwinSum(ListNode* head) {
ListNode* slow_ptr = head;
ListNode* fast_ptr = head;
while (fast_ptr && fast_ptr->next) {
fast_ptr = fast_ptr->next->next;
slow_ptr = slow_ptr->next;
}
ListNode *nextNode, *previous = NULL;
while (slow_ptr) {
nextNode = slow_ptr->next;
slow_ptr->next = previous;
previous = slow_ptr;
slow_ptr = nextNode;
}
int maxSum = 0;
ListNode* start_ptr = head;
while (previous) {
maxSum = max(maxSum, start_ptr->val + previous->val);
previous = previous->next;
start_ptr = start_ptr->next;
}
return maxSum;
}
};
The given C++ code defines a solution for finding the maximum twin sum in a linked list. A twin sum of a linked list is calculated by summing matching elements from the start and the reversed second half. This process involves finding a middle point, reversing the second half of the list, and then comparing nodes from both halves to find the maximum sum.
- Initialize two pointers,
slow_ptr
andfast_ptr
, from the head of the list to find the middle. Movefast_ptr
two steps andslow_ptr
one step untilfast_ptr
reaches the end. - Reverse the second half of the linked list starting from
slow_ptr
. Use an extra pointer,previous
, to keep track of the previous node while reversing. - Calculate the twin sums by iterating through the first half and the reversed second half simultaneously. Keep track of the maximum sum encountered during this process.
- Return the maximum sum obtained.
This solution efficiently computes the maximum twin sum with a time complexity of O(n), where n is the number of nodes in the linked list, and it operates with a space complexity of O(1), using only a few pointers.
class Solution {
public int maximumPairSum(ListNode head) {
ListNode slower = head;
ListNode faster = head;
// Find the middle point in the list
while (faster != null && faster.next != null) {
faster = faster.next.next;
slower = slower.next;
}
// Reverse the second part of the list
ListNode current, last = null;
while (slower != null) {
current = slower.next;
slower.next = last;
last = slower;
slower = current;
}
ListNode initializer = head;
int maxSum = 0;
while (last != null) {
maxSum = Math.max(maxSum, initializer.val + last.val);
last = last.next;
initializer = initializer.next;
}
return maxSum;
}
}
This solution tackles the problem of finding the maximum twin sum in a linked list using Java. The problem requires calculating the highest sum of pairs of values, where each pair consists of nodes symmetrically situated around the center of the list.
To handle this task, the method maximumPairSum(ListNode head)
is defined, which performs the following actions:
Firstly, initialize two pointers,
slower
andfaster
. Both start at the head of the linked list. Use these pointers to find the middle of the list. Thefaster
pointer moves at twice the speed of theslower
one, ensuring that whenfaster
reaches the end of the list,slower
is at the middle.Once the middle is found, the next part involves reversing the second half of the linked list starting from the
slower
pointer. This reversal is achieved through a classic linked list reversal process using temporary pointers.After reversing the second half of the list, start from the
head
of the list with a new pointer,initializer
, and iterate through the list comparing values from the start (initializer
) and the reversed second half (last
). Calculate the sum for each pair and update the maximum sum found (maxSum
) through this traversal.The loop continues until the end of the list is reached, ensuring that each twin sum has been calculated.
The method ultimately returns the maximum twin sum (maxSum
), successfully determining the highest sum of node values from symmetrically opposite ends of the linked list post reversal. This approach of twin pointers followed by a second-half reversal is efficient in terms of both time and space, effectively solving the problem within linear complexity.
No comments yet.