Reverse Nodes in Even Length Groups Problem
Description
LeetCode Problem 2074.
You are given the head of a linked list.
The nodes in the linked list are sequentially assigned to non-empty groups whose lengths form the sequence of the natural numbers (1, 2, 3, 4, ...). The length of a group is the number of nodes assigned to it. In other words,
- The
1^stnode is assigned to the first group. - The
2^ndand the3^rdnodes are assigned to the second group. - The
4^th,5^th, and6^thnodes are assigned to the third group, and so on.
Note that the length of the last group may be less than or equal to 1 + the length of the second to last group.
Reverse the nodes in each group with an even length, and return the head of the modified linked list.
Example 1:

Input: head = [5,2,6,3,9,1,7,3,8,4]
Output: [5,6,2,3,9,1,4,8,3,7]
Explanation:
- 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.
Example 2:

Input: head = [1,1,0,6]
Output: [1,0,1,6]
Explanation:
- 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.
Example 3:

Input: head = [1,1,0,6,5]
Output: [1,0,1,5,6]
Explanation:
- 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.
Constraints:
- The number of nodes in the list is in the range
[1, 10^5]. 0 <= Node.val <= 10^5
Solution
Approach: Group by group, reversing in place
Count the nodes first. The groups have lengths 1, 2, 3, ..., so the number of full groups is the largest l with l * (l + 1) / 2 <= n, and whatever is left over forms one last, shorter group.
Use a dummy node and a pointer prev standing just before the current group. For each group length l:
- if
lis even, reverse the nextlnodes and relink them afterprev; - then move
prevforward bylnodes to stand before the next group.
After the full groups, the leftover group has n - l * (l - 1) / 2 nodes. Reverse it too if that count is even. A helper reverse(head, l) reverses up to l nodes starting at head and reconnects the old head (now the tail) to the rest of the list.
Complexity: O(n) time, since every node is touched a constant number of times; O(1) extra space.
# 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]:
def reverse(head, l):
prev, cur, tail = None, head, head
i = 0
while cur and i < l:
t = cur.next
cur.next = prev
prev = cur
cur = t
i += 1
tail.next = cur
return prev
n = 0
t = head
while t:
t = t.next
n += 1
dummy = ListNode(0, head)
prev = dummy
l = 1
while (1 + l) * l // 2 <= n and prev:
if l % 2 == 0:
prev.next = reverse(prev.next, l)
i = 0
while i < l and prev:
prev = prev.next
i += 1
l += 1
left = n - l * (l - 1) // 2
if left > 0 and left % 2 == 0:
prev.next = reverse(prev.next, left)
return dummy.next