3
// Function to reverse a linked list4
ListNode *reverseList(ListNode *head) {5
if (!head) return head;8
ListNode *temp = head->next;17
ListNode *reverseEvenLengthGroups(ListNode *head) {18
// Creating a dummy node to avoid adding checks for the first node19
ListNode *dummy = new ListNode();22
ListNode *prev = dummy;24
// Loop to determine the lengths of groups25
for (int len = 1; len < 1e5 && head; len++) {26
ListNode *tail = head;29
// Determining the length of the current group30
// Its maximum length can be equal to len32
while (j < len && tail && tail->next) {37
// Head of the next group38
nextHead = tail->next;41
// If even sized group is found42
// Reversing the group and setting prev and head appropriately44
prev->next = reverseList(head);46
head->next = nextHead;49
// If group is odd sized, then simply going towards the next group