Use of Swap Nodes in Pairs Test
The solution approach to the problem is to swap adjacent pairs of nodes in a linked list. Let there be a funtion named as 'swapPairs' which takes a pointer to the head of a linked list head as input and returns a pointer to the head of the modified linked list. The function creates a new node pre with a value of 0 and sets its next pointer to the head of the linked list. This node will be used as a dummy node to handle the case where the head itself needs to be swapped. The function declares two pointers cur and tmp and initializes cur to the pre node. The cur pointer will be used to iterate through the linked list and swap adjacent pairs of nodes. The while loop runs as long as cur->next and cur->next->next are not nullptr, indicating that there are at least two more nodes to swap. Inside the while loop, the pointer tmp is assigned to cur->next, representing the first node of the current pair to be swapped. The next pointer of cur is updated to cur->next->next, effectively skipping the second node of the current pair. The next pointer of tmp is updated to tmp->next->next, effectively skipping the nodes after the second node of the current pair. The next pointer of cur->next is updated to tmp, linking the second node of the current pair to the first node. The cur pointer is updated to tmp, moving it forward to the next pair of nodes. Once the while loop finishes, the swapping process is completed, and the modified linked list is obtained. Finally, the function returns pre->next, which represents the head of the modified linked list.