Its simple you need to treat two nodes as one node and traverse.
Assuming the following is your node of the linklist
struct node
{
int data;
struct node *next;
};
Code to swap pairwise node
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;
}
}