Given a singly linked list, write a function to swap elements pairwise. For example, if the linked list is 1->2->3->4->5->6->7 then the function should change it to 2->1->4->3->6->5->7
Read full article from Pairwise swap elements of a given linked list by changing links | GeeksforGeeks
void pairWiseSwap(struct node **head){ // If linked list is empty or there is only one node in list if (*head == NULL || (*head)->next == NULL) return; // Initialize previous and current pointers struct node *prev = *head; struct node *curr = (*head)->next; *head = curr; // Change head before proceeeding // Traverse the list while (true) { struct node *next = curr->next; curr->next = prev; // Change next of current as previous node // If next NULL or next is the last node if (next == NULL || next->next == NULL) { prev->next = next; break; } // Change next of previous to next next prev->next = next->next; // Update previous and curr prev = next; curr = prev->next; }}