<p>You are given the <code>head</code> of a linked list.</p>

<p>The nodes in the linked list are <strong>sequentially</strong> assigned to <strong>non-empty</strong> groups whose lengths form the sequence of the natural numbers (<code>1, 2, 3, 4, ...</code>). The <strong>length</strong> of a group is the number of nodes assigned to it. In other words,</p>

<ul>
	<li>The <code>1<sup>st</sup></code> node is assigned to the first group.</li>
	<li>The <code>2<sup>nd</sup></code> and the <code>3<sup>rd</sup></code> nodes are assigned to the second group.</li>
	<li>The <code>4<sup>th</sup></code>, <code>5<sup>th</sup></code>, and <code>6<sup>th</sup></code> nodes are assigned to the third group, and so on.</li>
</ul>

<p>Note that the length of the last group may be less than or equal to <code>1 + the length of the second to last group</code>.</p>

<p><strong>Reverse</strong> the nodes in each group with an <strong>even</strong> length, and return <em>the</em> <code>head</code> <em>of the modified linked list</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/10/25/eg1.png" style="width: 699px; height: 124px;" />
<pre>
<strong>Input:</strong> head = [5,2,6,3,9,1,7,3,8,4]
<strong>Output:</strong> [5,6,2,3,9,1,4,8,3,7]
<strong>Explanation:</strong>
- The length of the first group is 1, which is odd, hence no reversal occurs.
- The length of the second group is 2, which is even, hence the nodes are reversed.
- The length of the third group is 3, which is odd, hence no reversal occurs.
- The length of the last group is 4, which is even, hence the nodes are reversed.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/10/25/eg2.png" style="width: 284px; height: 114px;" />
<pre>
<strong>Input:</strong> head = [1,1,0,6]
<strong>Output:</strong> [1,0,1,6]
<strong>Explanation:</strong>
- The length of the first group is 1. No reversal occurs.
- The length of the second group is 2. The nodes are reversed.
- The length of the last group is 1. No reversal occurs.
</pre>

<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/11/17/ex3.png" style="width: 348px; height: 114px;" />
<pre>
<strong>Input:</strong> head = [1,1,0,6,5]
<strong>Output:</strong> [1,0,1,5,6]
<strong>Explanation:</strong>
- The length of the first group is 1. No reversal occurs.
- The length of the second group is 2. The nodes are reversed.
- The length of the last group is 2. The nodes are reversed.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
	<li><code>0 &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 reverseEvenLengthGroups(self, head: Optional[ListNode]) -> Optional[ListNode]:
        '''
        keep track of number group number and nodes in group. reset number
        of nodes in group every time they are equal.
        the previous connector node is always the last node of an odd group.
        once an even group is reached, reverse the nodes:
        next_node = curr.next
        curr.next = prev
        prev = curr
        curr = next_node

        at the end of this, connect the first node to next node, and the connector 
        to the last node.

        ^^ wrong. not even group, even LENGTH groups
        '''
        
        # reverse nodfes and return new first node
        def reverseNodes(length, curr):
            prev = None
            i = 0
            first_node = curr
            while curr and i < length:
                next_node = curr.next
                curr.next = prev
                prev = curr
                curr = next_node
                i += 1
            
            first_node.next = curr
            
            return prev

        

        dummy = ListNode(None, head)
        curr = head
        connector = dummy
        first_node_in_group = head

        group = 1
        nodes = 0

        while curr:
            nodes += 1
            # if the group is even, reverse
            if nodes == 1:
                first_node_in_group = curr
            if nodes % 2 == 0:
                if nodes == group or curr.next == None:
                    new_first_node = reverseNodes(nodes, first_node_in_group)
                    connector.next = new_first_node
                    curr = first_node_in_group
                    connector = curr  
                    group += 1
                    nodes = 0
            # if the group is odd, keep iterating
            else:
                # if nodes == group, reassign connector, then nodes = 0 and groups += 1
                if nodes == group:
                    connector = curr    
                    group += 1
                    nodes = 0
            curr = curr.next
        
        return dummy.next

790