Your question is Reverse Adjacent Linked Nodes. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
Disney+ maintains a singly linked list of content identifiers for a playback sequence. Given the head of the list, reverse every two adjacent nodes and return the new head. The node values must remain unchanged, and only the links between nodes may be modified.
If the list contains an odd number of nodes, the final node remains in its original position. Implement the transformation in place without creating replacement nodes.
The input is head, either None or a reference to a ListNode object with fields val and next. Return the head of the modified linked list. The provided ListNode definition and test harness construct linked lists from the array representations used in the tests.
def reverse_pairs(head):