Given a singly linked list, write a function to swap elements pairwise.
Explanation and example:
If a linked list is 1 → 2 → 3 → 4 → 5
Then the output will be: 2 → 1 → 4 → 3 → 5
If the linked list is 1 → 2 → 3 → 4 → 5 → 6
Then the output will be: 2 → 1 → 4 → 3 → 6 → 5
Algorithm:
To solve this problem we firstly iterate the list from the head and take two consecutive nodes from that and swap the key values of them. Go for next two consecutive nodes. If there is only one node left then keep it same.
C++ implementation:
Output