java.lang.Object
g2101_2200.s2130_maximum_twin_sum_of_a_linked_list.Solution

public class Solution extends Object
2130 - Maximum Twin Sum of a Linked List.<p>Medium</p> <p>In a linked list of size <code>n</code>, where <code>n</code> is <strong>even</strong> , the <code>i<sup>th</sup></code> node ( <strong>0-indexed</strong> ) of the linked list is known as the <strong>twin</strong> of the <code>(n-1-i)<sup>th</sup></code> node, if <code>0 <= i <= (n / 2) - 1</code>.</p> <ul> <li>For example, if <code>n = 4</code>, then node <code>0</code> is the twin of node <code>3</code>, and node <code>1</code> is the twin of node <code>2</code>. These are the only nodes with twins for <code>n = 4</code>.</li> </ul> <p>The <strong>twin sum</strong> is defined as the sum of a node and its twin.</p> <p>Given the <code>head</code> of a linked list with even length, return <em>the <strong>maximum twin sum</strong> of the linked list</em>.</p> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2021/12/03/eg1drawio.png" alt="" /></p> <p><strong>Input:</strong> head = [5,4,2,1]</p> <p><strong>Output:</strong> 6</p> <p><strong>Explanation:</strong></p> <p>Nodes 0 and 1 are the twins of nodes 3 and 2, respectively. All have twin sum = 6.</p> <p>There are no other nodes with twins in the linked list.</p> <p>Thus, the maximum twin sum of the linked list is 6.</p> <p><strong>Example 2:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2021/12/03/eg2drawio.png" alt="" /></p> <p><strong>Input:</strong> head = [4,2,2,3]</p> <p><strong>Output:</strong> 7</p> <p><strong>Explanation:</strong></p> <p>The nodes with twins present in this linked list are:</p> <ul> <li> <p>Node 0 is the twin of node 3 having a twin sum of 4 + 3 = 7.</p> </li> <li> <p>Node 1 is the twin of node 2 having a twin sum of 2 + 2 = 4.</p> </li> </ul> <p>Thus, the maximum twin sum of the linked list is max(7, 4) = 7.</p> <p><strong>Example 3:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2021/12/03/eg3drawio.png" alt="" /></p> <p><strong>Input:</strong> head = [1,100000]</p> <p><strong>Output:</strong> 100001</p> <p><strong>Explanation:</strong></p> <p>There is only one node with a twin in the linked list having twin sum of 1 + 100000 = 100001.</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is an <strong>even</strong> integer in the range <code>[2, 10<sup>5</sup>]</code>.</li> <li><code>1 <= Node.val <= 10<sup>5</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • pairSum

      public int pairSum(ListNode head)