Jio service components may process ordered event nodes represented as singly linked lists. Given the head of a linked list, reverse its nodes two at a time and return the new head.
For each adjacent pair, swap the nodes themselves by changing pointers, not their stored values. If the list contains an odd number of nodes, leave the final node in its original position.
The input is head, either None or a reference to a singly linked ListNode with fields val and next. Return the head reference of the modified list. The input list must be rearranged in place, and no new data nodes may be created.
In the test cases, linked lists are represented as Python arrays for readability. The evaluation harness converts each array into a ListNode chain and converts the returned chain back into an array.
def reverse_pairs(head):