<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 &lt;= i &lt;= (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>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/03/eg1drawio.png" style="width: 250px; height: 70px;" />
<pre>
<strong>Input:</strong> head = [5,4,2,1]
<strong>Output:</strong> 6
<strong>Explanation:</strong>
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. 
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/03/eg2drawio.png" style="width: 250px; height: 70px;" />
<pre>
<strong>Input:</strong> head = [4,2,2,3]
<strong>Output:</strong> 7
<strong>Explanation:</strong>
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. 
</pre>

<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/03/eg3drawio.png" style="width: 200px; height: 88px;" />
<pre>
<strong>Input:</strong> head = [1,100000]
<strong>Output:</strong> 100001
<strong>Explanation:</strong>
There is only one node with a twin in the linked list having twin sum of 1 + 100000 = 100001.
</pre>

<p>&nbsp;</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 &lt;= Node.val &lt;= 10<sup>5</sup></code></li>
</ul>

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def pairSum(self, head: Optional[ListNode]) -> int:
        # Idea 1: convert to array, then use 2 pointers to traverse, and find max
        
        # arr = []
        # while head:
        #     arr.append(head.val)
        #     head = head.next
        # length = len(arr)
        # p0 = 0
        # p1 = length-1
        # largest = 0
        # while p0 < p1:
        #     largest = max(largest, arr[p1] + arr[p0])
        #     p0 += 1
        #     p1 -= 1
        # return largest


        # Idea 2: We don't know length of list, but we can measure it using a runner node.
        # During a single iteration, we append values to a list using p0, but p1 goes at twice the speed.
        # When p1 reaches the end, that's our signal that we're halfway through, so from now, every value of p0
        # is added to the corresponding values of the array
        # STEP 1: add values from p0 to array, and advance p1 at 2X nodes per step
        # STEP 2: once p1 reaches end, continue walking p0, but walk backwards through array, adding up values
        #   and keeping track of the largest one so far

        # arr = []
        # p0 = head
        # p1 = head

        # while p1:
        #     arr.append(p0.val)
        #     p0 = p0.next
        #     # no need to check due to eveness
        #     p1 = p1.next.next

        # largest = 0
        # # for i in range(len(arr)-1, -1, -1):
        # for i in reversed(range(len(arr))):
        #     # arr[i] += p0.val
        #     largest = max(largest, arr[i] + p0.val)
        #     p0 = p0.next

        # return largest


        # Idea 3 (neetcode): go to the middle of linked list, and reverse the first half. Then walk from
        # the center to the left and right, adding up values and keeping track of the max sum

        p0 = head
        p1 = head
        prev = None
        while p1:
            p1 = p1.next.next
            tmp = p0.next
            p0.next = prev
            prev = p0
            p0 = tmp

        # continuining right
        largest = 0
        while p0:
            largest = max(largest, p0.val + prev.val)
            p0 = p0.next
            prev = prev.next
        return largest


            
300