Given a&nbsp;linked list, swap every two adjacent nodes and return its head. You must solve the problem without&nbsp;modifying the values in the list's nodes (i.e., only nodes themselves may be changed.)

&nbsp;
Example 1:

Input: head = [1,2,3,4]
Output: [2,1,4,3]


Example 2:

Input: head = []
Output: []


Example 3:

Input: head = [1]
Output: [1]


&nbsp;
Constraints:


	The number of nodes in the&nbsp;list&nbsp;is in the range [0, 100].
	0 &lt;= Node.val &lt;= 100


class Solution:
    def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
        if not head:
            return head
        dummy = ListNode(None, head)
        prev, curr_1, curr_2 = dummy, head, head.next
        while curr_1 and curr_2:
            # 1. define temp nodes, temp nodes are comprised of 1 prev node, and multiple curr nodes. The while condition checks those curr nodes only.
            node_0 = prev
            node_1 = curr_1
            node_2 = curr_2
            node_3 = curr_2.next

            # 2. swap nodes using temp nodes
            node_0.next = node_2
            node_1.next = node_3
            node_2.next = node_1

            # 3. move temp nodes to the next window
            prev = node_1
            curr_1 = node_3
            curr_2 = node_3.next if node_3 else None

        return dummy.next
class Solution {
    public ListNode swapPairs(ListNode head) {
        ListNode dummy = new ListNode(0) , prev = dummy , curr = head;
        dummy.next = head;
        while(curr != null && curr.next != null){
            prev.next = curr.next;
            curr.next = curr.next.next ;
            prev.next.next = curr;
           curr = curr.next ;
            prev = prev.next.next;
        }
        return dummy.next;
    }
}
class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        // if head is NULL OR just having a single node, then no need to change anything
        if(head == NULL || head -> next == NULL)
        {
            return head;
        }

        ListNode* temp; // temporary pointer to store head -> next
        temp = head->next; // give temp what he want

        head->next = swapPairs(head->next->next); // changing links
        temp->next = head; // put temp -> next to head

        return temp; // now after changing links, temp act as our head
    }
};
var swapPairs = function(head) {
    if(!head || !head.next)  return head;
    
    // using an array to store all the pairs of the list 
    
    let ptrArr = [];
    let ptr = head;
    
    while(ptr) {
        ptrArr.push(ptr);
        let nptr = ptr.next ? ptr.next.next : null;
        if(nptr) ptr.next.next = null;
        ptr = nptr;
    }
    
    // reversing all the pairs of the list in array
    
    for(let i=0;i<ptrArr.length;i++) {
        let node = ptrArr[i];
        let temp = node.next;
        
        if(!temp) break;
        temp.next = node;
        node.next = null;
        ptrArr[i] = temp;
    }
    
    let dummy = new ListNode(-1);
    let nptr = dummy;
    
    // joining all the pairs back again
    ptrArr.forEach(ele => {
        nptr.next = ele;
        nptr = nptr.next.next;
    })
    
    return dummy.next;
    
};
Swap Nodes in Pairs